Archive:PHP and MySQL Programming/PHP Syntax: Difference between revisions

wikademia>Wikademia
 
(16 intermediate revisions by 4 users not shown)
Line 1: Line 1:
[[Programming:PHP and MySQL|<< Back to PHP and MySQL]]
== PHP tags: ==
== PHP tags: ==
 
{{ntnes}}
=== '''<?php [code] ?>''' ===
=== '''<?php [code] ?>''' ===


Line 30: Line 28:
  $name = "Bobby";
  $name = "Bobby";
  ?>
  ?>
 
  <?echo $name?>
  <?=$name?>
Which will output ''Bobby''.
Which will output ''Bobby''.


Line 91: Line 89:
e.g. : $b = 12;
e.g. : $b = 12;


e.g. : print “The value of a if $a.”; # Uses a as a String.
e.g. : print “The value of a is $a.”; # Uses a as a String.


e.g. : $c = $a + $b; # $a is now used as an Integer again.
e.g. : $c = $a + $b; # $a is now used as an Integer again.
Line 194: Line 192:
  $a = 1;
  $a = 1;
  $b = 10;
  $b = 10;
  if ($a > $b) {
  if ($a > $b) {
     echo “a is greater than b”;
     echo "a is greater than b";
  }
  }
  else if ($a == $b){
  else if ($a == $b) {
     echo “a is equal to b”;
     echo "a is equal to b";
  }
  }
  else{
  else {
     echo “a is not greater than b”;
     echo "a is not greater than b";
  }
  }
// OUTPUT:
// a is not greater than b
   
   


Line 208: Line 210:


  $a = 100;
  $a = 100;
  switch($a){
  switch($a) {
     case(10):
     case(10):
       echo “The value is 10”;
       echo "The value is 10";
       break;
       break;
     case(100):
     case(100):
       echo “The value is 100”;
       echo "The value is 100";
       break;
       break;
     case(1000):
     case(1000):
       echo “The value is 1000”;
       echo "The value is 1000";
       break;
       break;
     default:
     default:
       echo “Are you sure you entered in a valid number?”;
       echo "Are you sure you entered in a valid number?";
  }
  }
// OUTPUT:
// The value is 100




'''FOR statement'''
'''FOR statement'''


$a = 10;
  for ($i = 0; $i < 10; $i++) {
  for ($i = 0; $i < 10; $i++){
     # initialize $i ; while condition ; increment statement
     # initialize $i ; while condition ; increment statement
     print “$i”;
     echo $i;
  }
  }
// OUTPUT:
// 0123456789




Line 235: Line 246:


  $a = array(1, 2, 3, 4, 5);
  $a = array(1, 2, 3, 4, 5);
  foreach ($a as $val){
  foreach ($a as $val){
     echo “$val”;
     echo $val;
  }
  }
// OUTPUT:
// 12345




Line 258: Line 273:
  # least once!
  # least once!


== Functions ==


== FUNCTIONS ==
  function square_number($number) {
 
     return ($number * $number);
  function my_function($value){
     if (!$value){
      return 0;
    }
    else{
      return ($value % 2);
    }
  }
  }
  $a = 45;
  $ans = my_function($a);
  $answer = square_number(10);
  echo "The answer is {$answer}";
// OUTPUT:
// The answer is 100


== Classes ==
== Classes ==


  class my_class {
  class dog {
     var $value1;
     var $name;
     var $value2;
      
     function set_value1($val){
     function dog($name){
       $this->value1 = $val;
       $this->name = $name;
     }
     }
     function set_value2($val){
   
       if ($this->value1 > 0){
     function bark(){
          $this->value2 = $val;
       echo "Woof! Woof!";
      }
    }
       else{
   
          $this->value2 = 0;
    function who_am_i() {
      }
       echo "My name is {$this->name), and I am a dog";
     }
     }
  }
  }
  $new_class = &new my_class;
$new_class->set_value1(10);
  $the_dog = new dog("John");
  $new_class->set_value2(2);
  $the_dog->who_am_i();
 
 
// OUTPUT:
[[Programming:PHP and MySQL:Introduction|<< 1. Introduction]] | [[Programming:PHP and MySQL|TOC]] | [[Programming:PHP and MySQL:File Handling|>> 3. File Handling]]
// My name is John, and I am a dog


[[Category:PHP]]
[[Category:PHP and MySQL Programming|{{SUBPAGENAME}}]]
[[Category:MySQL]]