Jump to content

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

From IdeaWazaWiki
wikademia>The Kid
New page: == PHP tags: == === '''<?php [code] ?>''' === * Enclose php code * Can be configured in the php.ini to be <? ?>, <script language="php"> </script> etc. * Embedded in normal HTML code ...
 
wikademia>Wikademia
 
(16 intermediate revisions by 6 users not shown)
Line 1: Line 1:
== PHP tags: ==
== PHP tags: ==
 
{{ntnes}}
=== '''<?php [code] ?>''' ===
=== '''<?php [code] ?>''' ===


* Enclose php code
* Enclose php code


* Can be configured in the php.ini to be <? ?>, <script language="php"> </script> etc.
* Can be configured in the <code>php.ini</code> to be <code><? ?></code>, <code><script language="php"> </script></code> etc.


* Embedded in normal HTML code
* Embedded in normal HTML code
Line 14: Line 13:
Example:  
Example:  


<source lang="php">
  <?php
  <?php
  print “Hello World\n”;
  print "Hello World\n";
  $date = date(“Y-m-d H:i:s”);
  $date = date("Y-m-d H:i:s");
  print “The date and time at the moment is $date”;
  print "The date and time at the moment is $date";
  ?>
  ?>
</source>


=== '''<?=''' ===
=== '''<?=''' ===
Line 26: Line 27:


Example:   
Example:   
<source lang="php">
  <?php
  <?php
  $name = "Bobby";
  $name = "Bobby";
Line 31: Line 34:
   
   
  <?=$name?>
  <?=$name?>
</source>
Which will output ''Bobby''.
Which will output ''Bobby''.


Example:
Example:
  <?=date(“Y-m-d H:i:s”)?>  
 
<source lang="php">
  <?=date("Y-m-d H:i:s")?>
</source>


Which will output something like ''2005-11-09 19:40:05''.
Which will output something like ''2005-11-09 19:40:05''.
[[]]


== Commenting ==
== Commenting ==
Line 45: Line 54:


* Example:
* Example:
  echo “Hello”; # Everything from the hash is commented out
<source lang="php">
  echo "Hello"; # Everything from the hash is commented out
</source>


* Example:
* Example:
<source lang="php">
  # This entire line is commented out
  # This entire line is commented out
</source>


==='''//'''===
==='''//'''===
Line 61: Line 74:


* Example:  
* Example:  
<source lang="php">
  /*All of this is
  /*All of this is
  commented out.
  commented out.
  Even this line!*/
  Even this line!*/
</source>


== Variables ==
== Variables ==
Line 69: Line 84:
➢ Variables in PHP are denoted by the $ prefix.
➢ Variables in PHP are denoted by the $ prefix.


e.g. : $a = “Hello World”; # this assigns the string “Hello World” to $a.
* Example:
 
<source lang="php">
e.g. : $b = “$a, I'm Ralfe”; # this assigns “Hello World, I'm Ralfe” to $b.
$a = “Hello World”; # this assigns the string “Hello World” to $a.
 
$b = “$a, I'm Ralfe”; # this assigns “Hello World, I'm Ralfe” to $b.
e.g. : $b = $a.”, I'm Ralfe”; # exactly the same as the previous example.
$b = $a.”, I'm Ralfe”; # exactly the same as the previous example.
 
</source>


➢ PHP supports dynamic variables.
➢ PHP supports dynamic variables.


e.g. : $c = “response”;
* Example:
<source lang="php">
$c = “response”;


e.g. : $$c = “Hello Ralfe”; # this assigns “Hello Ralfe” to $response.
$$c = “Hello Ralfe”; # this assigns “Hello Ralfe” to $response.
</source>




Line 86: Line 104:
definition. PHP handles all data type conversions.
definition. PHP handles all data type conversions.


e.g. : $a = 4;
* Example:
<source lang="php">
$a = 4;


e.g. : $b = 12;
$b = 12;


e.g. : print “The value of a is $a.”; # Uses a as a String.
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.


$c = $a + $b; # $a is now used as an Integer again.
</source>


➢ PHP supports boolean variables, which can be assigned either a one or a zero, or a
➢ PHP supports boolean variables, which can be assigned either a one or a zero, or a
true or false.
true or false.


e.g. : $a = true;
Example:
<source lang="php">
$a = true;


e.g. : $b = 1;
$b = 1; # $a and $b are the same thing!


e.g. : # $a and $b are the same thing!
$c = false;


e.g. : $c = false;
$d = 0; # $c and $d are the same thing!
 
</source>
e.g. : $d = 0;
 
e.g. : # $c and $d are the same thing!


== Operators ==
== Operators ==
Line 114: Line 133:
➢ Arithmetic Operators
➢ Arithmetic Operators


* Example:
<source lang="php">
$a = 4;
$a = 4;


$b = 2;
$b = 2;


// Addition: $a + $b = 6
$a + $b = 6;  // Addition


// Subtraction: $a - $b = 2
$a - $b = 2;  // Subtraction


// Multiplication: $a * $b = 8
$a * $b = 8;  // Multiplication


// Division: $a / $b = 2
$a / $b = 2;  // Division


// Modulus: $a % $b = 0
$a % $b = 0;  // Modulus


// Increment: $a++
$a++;  // Increment


// Decrement: $a--
$a--;  // Decrement
</source>




➢ Assignment Operators
➢ Assignment Operators


* Example:
<source lang="php">
$a = 4;
$a = 4;


Line 140: Line 164:


// $b = 4;
// $b = 4;
 
</source>


➢ Comparison Operators
➢ Comparison Operators


* Example:
<source lang="php">
$a == $b // test if two values are equal
$a == $b // test if two values are equal


Line 155: Line 181:


$a >= $b // test if the first value is greater than or equal to the second
$a >= $b // test if the first value is greater than or equal to the second
</source>


== Concatenation ==
== Concatenation ==


* Example:
<source lang="php">
$a = "Fill the halls ";


$a = “Fill the halls “;
$b = "with poisoned ivy...";


$b = “with poisoned ivy...“;
$c = $a . $b;  # the '.' operator concatenates two variables.


$c = $a . $b; # the '.' operator concatenates two variables.
// $c = "Fill the halls with poisoned ivy...";
 
</source>
// $c = “Fill the halls with poisoned ivy...”


== Arrays ==
== Arrays ==
Line 171: Line 200:
PHP supports both numerically indexed arrays as well as associative arrays.
PHP supports both numerically indexed arrays as well as associative arrays.


* Example:
<source lang="php">
$a = array(1, 2, 3, 4);
$a = array(1, 2, 3, 4);


Line 181: Line 212:
// $a[3] = 4;
// $a[3] = 4;


$b = array(“name” => ”Fred”, “age” => 30);
$b = array("name" => "Fred", "age" => 30);


// $b['name'] = “Fred”;
// $b['name'] = "Fred";


// $b['age'] = 30;
// $b['age'] = 30;
</source>


== Decision and Loop Statements ==
== Decision and Loop Statements ==
Line 191: Line 223:
'''IF THEN ELSE statement'''
'''IF THEN ELSE statement'''


* Example:
<source lang="php">
  $a = 1;
  $a = 1;
  $b = 10;
  $b = 10;
Line 206: Line 240:
  // OUTPUT:
  // OUTPUT:
  // a is not greater than b
  // a is not greater than b
</source>


'''SWITCH statement'''
'''SWITCH statement'''


* Example:
<source lang="php">
  $a = 100;
  $a = 100;
   
   
Line 231: Line 267:
  // OUTPUT:
  // OUTPUT:
  // The value is 100
  // The value is 100
 
</source>


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


* Example:
<source lang="php">
  for ($i = 0; $i < 10; $i++) {
  for ($i = 0; $i < 10; $i++) {
     # initialize $i ; while condition ; increment statement
     # initialize $i ; while condition ; increment statement
Line 242: Line 280:
  // OUTPUT:
  // OUTPUT:
  // 0123456789
  // 0123456789
 
</source>


'''FOREACH statement'''
'''FOREACH statement'''


* Example:
<source lang="php">
  $a = array(1, 2, 3, 4, 5);
  $a = array(1, 2, 3, 4, 5);
   
   
Line 254: Line 294:
  // OUTPUT:
  // OUTPUT:
  // 12345
  // 12345
 
</source>


'''WHILE statement'''
'''WHILE statement'''


* Example:
<source lang="php">
  while ($row = mysql_fetch_row($result)){
  while ($row = mysql_fetch_row($result)){
     print $row[0];
     print $row[0];
  }
  }
 
</source>


'''DO WHILE statement'''
'''DO WHILE statement'''


* Example:
<source lang="php">
  $i = 0;
  $i = 0;
  # Note that it might seem that $i will
  # Note that it might seem that $i will
Line 273: Line 317:
  } while ($i >0);
  } while ($i >0);
  # least once!
  # least once!
</source>


== Functions ==
== Functions ==


* Example:
<source lang="php">
  function square_number($number) {
  function square_number($number) {
     return ($number * $number);
     return ($number * $number);
Line 285: Line 332:
  // OUTPUT:
  // OUTPUT:
  // The answer is 100
  // The answer is 100
</source>


== Classes ==
== Classes ==


* Example:
<source lang="php">
  class dog {
  class dog {
     var $name;
     var $name;
Line 300: Line 350:
      
      
     function who_am_i() {
     function who_am_i() {
       echo "My name is {$this->name), and I am a dog";
       echo "My name is {$this->name}, and I am a dog";
     }
     }
  }
  }
Line 309: Line 359:
  // OUTPUT:
  // OUTPUT:
  // My name is John, and I am a dog
  // My name is John, and I am a dog
</source>
[[../Introduction to PHP/|Previous: Introduction to PHP]] | [[../|TOC]] | [[../File Handling/|Next: File Handling]]


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

Latest revision as of 07:37, 12 June 2009

PHP tags:

<?php [code] ?>

  • Enclose php code
  • Can be configured in the php.ini to be <? ?>, <script language="php"> </script> etc.
  • Embedded in normal HTML code
  • Within php tags, statements are separated by a ; (generally also followed by a new line).

Example:

<source lang="php">

<?php
print "Hello World\n";
$date = date("Y-m-d H:i:s");
print "The date and time at the moment is $date";
?>

</source>

<?=

  • Used if you just want to print out the value of a variable

Example:

<source lang="php">

<?php
$name = "Bobby";
?>

<?=$name?>

</source>

Which will output Bobby.

Example:

<source lang="php">

<?=date("Y-m-d H:i:s")?> 

</source>

Which will output something like 2005-11-09 19:40:05. [[]]

Commenting

#

  • Comments out one line
  • Example:

<source lang="php">

echo "Hello"; # Everything from the hash is commented out

</source>

  • Example:

<source lang="php">

# This entire line is commented out

</source>

//

  • Works just as in Java and JavaScript
  • Same function as #

/* (text) */

  • Comments out everything between the /* and the */
  • Example:

<source lang="php">

/*All of this is
commented out.
Even this line!*/

</source>

Variables

➢ Variables in PHP are denoted by the $ prefix.

  • Example:

<source lang="php">

$a = “Hello World”; # this assigns the string “Hello World” to $a.
$b = “$a, I'm Ralfe”; # this assigns “Hello World, I'm Ralfe” to $b.
$b = $a.”, I'm Ralfe”; # exactly the same as the previous example.

</source>

➢ PHP supports dynamic variables.

  • Example:

<source lang="php"> $c = “response”;

$$c = “Hello Ralfe”; # this assigns “Hello Ralfe” to $response. </source>


➢ PHP variables do not have to be declared ahead of time, nor do they require a type definition. PHP handles all data type conversions.

  • Example:

<source lang="php"> $a = 4;

$b = 12;

print “The value of a is $a.”; # Uses a as a String.

$c = $a + $b; # $a is now used as an Integer again. </source>

➢ PHP supports boolean variables, which can be assigned either a one or a zero, or a true or false.

Example: <source lang="php"> $a = true;

$b = 1; # $a and $b are the same thing!

$c = false;

$d = 0; # $c and $d are the same thing! </source>

Operators

➢ Arithmetic Operators

  • Example:

<source lang="php"> $a = 4;

$b = 2;

$a + $b = 6; // Addition

$a - $b = 2; // Subtraction

$a * $b = 8; // Multiplication

$a / $b = 2; // Division

$a % $b = 0; // Modulus

$a++; // Increment

$a--; // Decrement </source>


➢ Assignment Operators

  • Example:

<source lang="php"> $a = 4;

$b = $a;

// $b = 4; </source>

➢ Comparison Operators

  • Example:

<source lang="php"> $a == $b // test if two values are equal

$a != $b // test if two values are not equal

$a < $b // test if the first value is less than the second

$a > $b // test if the first value is greater than the second

$a <= $b // test if the first value is less than or equal to the second

$a >= $b // test if the first value is greater than or equal to the second </source>

Concatenation

  • Example:

<source lang="php"> $a = "Fill the halls ";

$b = "with poisoned ivy...";

$c = $a . $b; # the '.' operator concatenates two variables.

// $c = "Fill the halls with poisoned ivy..."; </source>

Arrays

PHP supports both numerically indexed arrays as well as associative arrays.

  • Example:

<source lang="php"> $a = array(1, 2, 3, 4);

// $a[0] = 1;

// $a[1] = 2;

// $a[2] = 3;

// $a[3] = 4;

$b = array("name" => "Fred", "age" => 30);

// $b['name'] = "Fred";

// $b['age'] = 30; </source>

Decision and Loop Statements

IF THEN ELSE statement

  • Example:

<source lang="php">

$a = 1;
$b = 10;

if ($a > $b) {
   echo "a is greater than b";
}
else if ($a == $b) {
   echo "a is equal to b";
}
else {
   echo "a is not greater than b";
}

// OUTPUT:
// a is not greater than b

</source>

SWITCH statement

  • Example:

<source lang="php">

$a = 100;

switch($a) {
   case(10):
      echo "The value is 10";
      break;

   case(100):
      echo "The value is 100";
      break;

   case(1000):
      echo "The value is 1000";
      break;

   default:
      echo "Are you sure you entered in a valid number?";
}

// OUTPUT:
// The value is 100

</source>

FOR statement

  • Example:

<source lang="php">

for ($i = 0; $i < 10; $i++) {
   # initialize $i ; while condition ; increment statement
   echo $i;
}

// OUTPUT:
// 0123456789

</source>

FOREACH statement

  • Example:

<source lang="php">

$a = array(1, 2, 3, 4, 5);

foreach ($a as $val){
   echo $val;
}

// OUTPUT:
// 12345

</source>

WHILE statement

  • Example:

<source lang="php">

while ($row = mysql_fetch_row($result)){
   print $row[0];
}

</source>

DO WHILE statement

  • Example:

<source lang="php">

$i = 0;
# Note that it might seem that $i will
do{
   # never be printed to the screen, but
   print $i;
   # a DO WHILE loop always executes at
} while ($i >0);
# least once!

</source>

Functions

  • Example:

<source lang="php">

function square_number($number) {
   return ($number * $number);
}

$answer = square_number(10);
echo "The answer is {$answer}";

// OUTPUT:
// The answer is 100

</source>

Classes

  • Example:

<source lang="php">

class dog {
   var $name;
   
   function dog($name){
      $this->name = $name;
   }
   
   function bark(){
      echo "Woof! Woof!";
   }
   
   function who_am_i() {
      echo "My name is {$this->name}, and I am a dog";
   }
}

$the_dog = new dog("John");
$the_dog->who_am_i();

// OUTPUT:
// My name is John, and I am a dog

</source>

[[../Introduction to PHP/|Previous: Introduction to PHP]] | [[../|TOC]] | [[../File Handling/|Next: File Handling]]