Thursday, 1 September 2016

Php

PHP


<p>
   <?php
    echo "papa"; ------ PHP code within a paragraph
   ?>
</p>



<?php
   echo "Hello," . " " . "world" . "!"; ---- Concatanation operator in PHP
?>


<?php
   $myName="Venkat"; --- Variable declaration 
?>


      <?php
        $items = 7;    
        
        if($items > 5) {   -----------------------If-else condition in php
          echo "You get a 10% discount!";
        }
         else {
            echo "You get a 5% discount!";
        }
      ?>


    <?php
    switch (2) {
        case 0:
            echo 'The value is 0';
            break;
        case 1:
            echo 'The value is 1'; -------- Switch statement
            break;
        case 2:
            echo 'The value is 2';
            break;
        default:
            echo "The value isn't 0, 1 or 2";
    }
    ?>




    <?php
      $array = array("Egg", "Tomato", "Beans"); --- Array in php
    ?>   



    <?php
        $tens = array(10, 20, 30, 40, 50);  ---- print array element
        print $tens{2};
        echo $tens[2];
      ?>



      <?php
        $languages = array("HTML/CSS",
        "JavaScript", "PHP", "Python", "Ruby");  ---modiying elements of array
        $languages[1]="lisp";
        echo $languages[1];
        unset($languages[3]); ----- to remove array elements
      ?>



   <?php
      for ($leap = 2004; $leap < 2050; $leap = $leap + 4) { ----- for loop in php
        echo "<p>$leap</p>";
      }
    ?>



<html>
  <head>
    <link rel="stylesheet" href="stylesheet.css" />
    <title>Codecademy Languages</title>
  </head>
  <body>
    <h1>Languages you can learn on Codecademy:</h1>   ----- This is how you use PHP inside a html doc and use CSS around the html tags
    <div class="wrapper">
      <ul>
        <?php
          $langs = array("JavaScript",
          "HTML/CSS", "PHP",
          "Python", "Ruby");
        
          foreach ($langs as $lang) {
              echo "<li>$lang</li>";
          }
        
          unset($lang);
        ?>
      </ul>
    </div>
  </body>
</html>




      <?php
        $sentence = array("I'm ", "learning ", "PHP!");  ---- foreach in php for array 
        
        foreach($sentence as $word){
          echo $word;
        }
      ?>



  <?php
//Add while loop below --while loop in php
    while()
    endwhile;
    ?>


    <?php
$i = 0;
do {
    echo $i;
} while ($i > 0); --- do while loop o php
?>


    <?php
    // Get the length of your own name
    // and print it to the screen!
  $length = strlen("venkat"); -------- to get the lenght of a string
  print $length;
    ?>