Jump to content

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

From IdeaWazaWiki
wikademia>Ralfe
wikademia>Wikademia
 
(34 intermediate revisions by 8 users not shown)
Line 1: Line 1:
== PHP tags: ==
{{ntnes}}
=== '''<?php [code] ?>''' ===


== PHP tags : ==
* Enclose php code


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


– Enclose php code
* Embedded in normal HTML code
– Can be configured in the php.ini file to be anything (i.e. : <%%>)
– Embedded in normal HTML code
– Within php tags, statements are separated by ; .
– e.g. :


<?
* Within php tags, statements are separated by a ; (generally also followed by a new line).
print “Hello World\n”;
$date = date(“Y-m-d H:i:s”);
print “The date and time at the moment is $date”;
?>


'''<?='''
Example:


– Used if you just want to print out the value of a variable
  <?php
– e.g. : <?= $name ?>
print “Hello World\n”;
prints out bobby.
$date = date(“Y-m-d H:i:s”);
– e.g. : <?= date(“Y-m-d H:i:s”) ?>  
print “The date and time at the moment is $date”;
prints out 2005-11-09 19:40:05.
?>


=== '''<?=''' ===
* Used if you just want to print out the value of a variable
Example: 
<?php
$name = "Bobby";
?>
<?=$name?>
Which will output ''Bobby''.
Example:
<?=date(“Y-m-d H:i:s”)?>
Which will output something like ''2005-11-09 19:40:05''.


== Commenting ==
== Commenting ==


'''#'''
==='''#'''===
 
* Comments out one line


– Comments out one line
* Example:
– e.g. : echo “Hello”; # everything from the hash is commented out
echo “Hello”; # Everything from the hash is commented out
– e.g. : # This entire line is commented out


'''//'''
* Example:
# This entire line is commented out


– Works just as in Java and JavaScript
==='''//'''===
– Exactly the same as the #


'''/*....*/'''
* Works just as in Java and JavaScript


– Comments out everything between the /* and the */
* Same function as '''#'''
– e.g. : /*All of this is
commented out.
Even this line!*/


==='''/* (text) */'''===
* Comments out everything between the /* and the */
* Example:
/*All of this is
commented out.
Even this line!*/


== Variables ==
== Variables ==
Line 51: Line 69:


e.g. : $a = “Hello World”; # this assigns the string “Hello World” to $a.
e.g. : $a = “Hello World”; # this assigns the string “Hello World” to $a.
e.g. : $b = “$a, I'm Ralfe”; # this assigns “Hello World, I'm Ralfe” to $b.
e.g. : $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.
e.g. : $b = $a.”, I'm Ralfe”; # exactly the same as the previous example.


➢ PHP supports dynamic variables.
➢ PHP supports dynamic variables.
e.g. : $c = “response”;
e.g. : $c = “response”;
e.g. : $$c = “Hello Ralfe”; # this assigns “Hello Ralfe” to $response.
e.g. : $$c = “Hello Ralfe”; # this assigns “Hello Ralfe” to $response.


➢ PHP variables do not have to be declared ahead of time, nor do they require a type
➢ PHP variables do not have to be declared ahead of time, nor do they require a type
Line 62: Line 86:


e.g. : $a = 4;
e.g. : $a = 4;
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.


➢ 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
Line 70: Line 98:


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


== Operators ==


== Operators ==
➢ Arithmetic Operators


➢
Arithmetic Operators
$a = 4;
$a = 4;
 
$b = 2;
$b = 2;
 
// Addition: $a + $b = 6
// Addition: $a + $b = 6
 
// Subtraction: $a - $b = 2
// Subtraction: $a - $b = 2
 
// Multiplication: $a * $b = 8
// Multiplication: $a * $b = 8
 
// Division: $a / $b = 2
// Division: $a / $b = 2
 
// Modulus: $a % $b = 0
// Modulus: $a % $b = 0
 
// Increment: $a++
// Increment: $a++
 
// Decrement: $a--
// Decrement: $a--
➢
 
Assignment Operators
 
➢ Assignment Operators
 
$a = 4;
$a = 4;
 
$b = $a;
$b = $a;
 
// $b = 4;
// $b = 4;
➢
Comparison Operators
$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
➢


➢ Comparison Operators
$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


== Concatenation ==
== Concatenation ==


 
$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...”
// $c = “Fill the halls with poisoned ivy...”


== Arrays ==
== Arrays ==


PHP supports both numerically indexed arrays as well as associative arrays.
PHP supports both numerically indexed arrays as well as associative arrays.
$a = array(1, 2, 3, 4);
$a = array(1, 2, 3, 4);
// $a[0] = 1;
// $a[0] = 1;
// $a[1] = 2;
// $a[1] = 2;
// $a[2] = 3;
// $a[2] = 3;
// $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;


== Decision and Loop Statements ==
'''IF THEN ELSE statement'''
$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
'''SWITCH statement'''
$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


== Decision and Loop Statements ==


# --- IF THEN ELSE statement ---
'''FOR statement'''
$a = 1;
 
$b = 10;
for ($i = 0; $i < 10; $i++) {
if ($a > $b) {
    # initialize $i ; while condition ; increment statement
echo “a is greater than b”;
    echo $i;
}
}
else if ($a == $b){
echo “a is equal to b”;
// OUTPUT:
}
// 0123456789
else{
 
echo “a is not greater than b”;
 
}
'''FOREACH statement'''
# --- SWITCH statement ---
 
$a = 100;
$a = array(1, 2, 3, 4, 5);
switch($a){
case(10):
foreach ($a as $val){
echo “The value is 10”;
    echo $val;
break;
}
case(100):
echo “The value is 100”;
// OUTPUT:
break;
// 12345
case(1000):
 
echo “The value is 1000”;
 
break;
'''WHILE statement'''
default:
 
echo “Are you sure you entered in a valid number?”;
while ($row = mysql_fetch_row($result)){
}
    print $row[0];
# --- FOR statement ---
}
$a = 10;
 
for ($i = 0; $i < 10; $i++){
 
# initialize $i ; while condition ; increment statement
'''DO WHILE statement'''
print “$i”;
 
}
$i = 0;
Page 4
# Note that it might seem that $i will
# --- FOREACH statement ---
do{
$a = array(1, 2, 3, 4, 5);
    # never be printed to the screen, but
foreach ($a as $val){
    print $i;
echo “$val”;
    # a DO WHILE loop always executes at
}
} while ($i >0);
# --- WHILE statement ---
# least once!
while ($row = mysql_fetch_row($result)){
 
print $row[0];
== Functions ==
}
# --- DO WHILE statement ---
$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!
FUNCTIONS
function my_function($value){
if (!$value){
return 0;
}
else{
return ($value % 2);
}
}
$a = 45;
$ans = my_function($a);


function square_number($number) {
    return ($number * $number);
}
$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:
// My name is John, and I am a dog
 
[[Category:PHP and MySQL Programming|{{SUBPAGENAME}}]]

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:

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

<?=

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

Example:

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

<?=$name?>

Which will output Bobby.

Example:

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

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

Commenting

#

  • Comments out one line
  • Example:
echo “Hello”; # Everything from the hash is commented out
  • Example:
# This entire line is commented out

//

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

/* (text) */

  • Comments out everything between the /* and the */
  • Example:
/*All of this is
commented out.
Even this line!*/

Variables

➢ Variables in PHP are denoted by the $ prefix.

e.g. : $a = “Hello World”; # this assigns the string “Hello World” to $a.

e.g. : $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.


➢ PHP supports dynamic variables.

e.g. : $c = “response”;

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


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

e.g. : $a = 4;

e.g. : $b = 12;

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.


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

e.g. : $a = true;

e.g. : $b = 1;

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

e.g. : $c = false;

e.g. : $d = 0;

e.g. : # $c and $d are the same thing!

Operators

➢ Arithmetic Operators

$a = 4;

$b = 2;

// Addition: $a + $b = 6

// Subtraction: $a - $b = 2

// Multiplication: $a * $b = 8

// Division: $a / $b = 2

// Modulus: $a % $b = 0

// Increment: $a++

// Decrement: $a--


➢ Assignment Operators

$a = 4;

$b = $a;

// $b = 4;


➢ Comparison Operators

$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

Concatenation

$a = “Fill the halls “;

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

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

// $c = “Fill the halls with poisoned ivy...”

Arrays

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

$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;

Decision and Loop Statements

IF THEN ELSE statement

$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

SWITCH statement

$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


FOR statement

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

// OUTPUT:
// 0123456789


FOREACH statement

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

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

// OUTPUT:
// 12345


WHILE statement

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


DO WHILE statement

$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!

Functions

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

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

// OUTPUT:
// The answer is 100

Classes

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