Archive:PHP and MySQL Programming/PHP Syntax: Difference between revisions
wikademia>Wikademia m moved PHP and MySQL Programming/PHP Syntax to Archive:PHP and MySQL Programming/PHP Syntax over redirect |
|||
| (16 intermediate revisions by 4 users not shown) | |||
| Line 1: | Line 1: | ||
== PHP tags: == | == PHP tags: == | ||
{{ntnes}} | |||
=== '''<?php [code] ?>''' === | === '''<?php [code] ?>''' === | ||
| Line 30: | Line 28: | ||
$name = "Bobby"; | $name = "Bobby"; | ||
?> | ?> | ||
<? | <?=$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 | 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 | echo "a is greater than b"; | ||
} | } | ||
else if ($a == $b){ | else if ($a == $b) { | ||
echo | echo "a is equal to b"; | ||
} | } | ||
else{ | else { | ||
echo | 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 | echo "The value is 10"; | ||
break; | break; | ||
case(100): | case(100): | ||
echo | echo "The value is 100"; | ||
break; | break; | ||
case(1000): | case(1000): | ||
echo | echo "The value is 1000"; | ||
break; | break; | ||
default: | default: | ||
echo | echo "Are you sure you entered in a valid number?"; | ||
} | } | ||
// OUTPUT: | |||
// The value is 100 | |||
'''FOR statement''' | '''FOR statement''' | ||
for ($i = 0; $i < 10; $i++) { | |||
for ($i = 0; $i < 10; $i++){ | |||
# initialize $i ; while condition ; increment statement | # initialize $i ; while condition ; increment statement | ||
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 | echo $val; | ||
} | } | ||
// OUTPUT: | |||
// 12345 | |||
| Line 258: | Line 273: | ||
# least once! | # least once! | ||
== Functions == | |||
function square_number($number) { | |||
return ($number * $number); | |||
function | |||
} | } | ||
$ | |||
$ | $answer = square_number(10); | ||
echo "The answer is {$answer}"; | |||
// OUTPUT: | |||
// The answer is 100 | |||
== Classes == | == Classes == | ||
class | class dog { | ||
var $ | var $name; | ||
function | function dog($name){ | ||
$this-> | $this->name = $name; | ||
} | } | ||
function | |||
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 | |||
[[Category:PHP | [[Category:PHP and MySQL Programming|{{SUBPAGENAME}}]] | ||