Home
XHTML
CSS
PHP
MySQL
SEO
JavaScript
Computer Basics
Number Systems
LINUX


PHP Functions

PHP | Values | Operators | Functions | Arrays | Forms | Sessions-Cookies | Classes

Functions | Loop Functions | Standard Functions | User Functions


Functions are self-contained blocks of code that can be reused. A function makes a decision depending on the evaluation of a condition. A condition is declared within  ( )  parenthesis. A function evaluates a condition then generates an action.

The  if()  function evaluates whether a condition is true or false. If the condition evaluates true, the statement is executed. If the condition evaluates false, the statement is not executed...

<?php

$number=15;

if($number > 12)
echo "15 is greater than 12.";

?>

The condition is true. The output is...

15 is greater than 12.



This is another example. Multiple statements are declared within  { }  curly brackets...

<?php

$number=18;

if($number == 18)
{echo '18 is equal to 18.';
echo ' I am a genius!';}

?>

The condition is true. The output is...

18 is equal to 18. I am a genius!



This is another example...

<?php

$age=36;

if($age > 24 && $age < 48)
{echo 'Between 24 and 36.';}

?>

Both conditions are true. The output is...

Between 24 and 36.



This is another example...

<?php

$age=10;

if($age > 20 || $age < 40)
{echo 'Either greater than 20, or less than 40, or both.';}

?>

The condition is one or the other, or both. The output is...

Either greater than 20, or less than 40, or both.



This is another example...

<?php

$age=10;

if(!($age > 20))
{echo 'Not greater than 20.';}

?>

The condition is false. The output is...

Not greater than 20.




The else command generates alternative statements, one if the condition is true, or another if the condition is false. The  { }  curly brackets are not required for single statements, but are used for readability...

<?php

$number=2;

if($number > 6)
{echo 'Greater than 6.';}
else
{echo 'Not greater than 6.';}

?>

The condition is false. The output is...

Not greater than 6.




The  elseif()  function adds another condition to the  if()  function...

<?php

$number=2;

if($number > 6)
{echo 'Greater than 6.';}
elseif
($number == 6)
{echo 'Equal to 6.';}
else
echo 'Less than 6.';

?>

Both conditions are false. The output is...

Less than 6.




A statement block can be declared within another statement block...

<?php

$number=9;

if($number > 6)
{if($number > 12)
{echo 'Greater than 12.';}
else
{echo 'Greater than 6 but less than 12.';}}

?>

The output is...

Greater than 6 but less than 12.




There is an alternative to evaluating a condition with ternary operators. The echo command holds a condition until the following statement is evaluated. The  ?  question mark is an operator that processes like the  if()  function and the  :  colon is an operator that processes like the else command...

<?php

$num=12;

echo ($num == 9) ? 'Equal to 9.' : 'Not equal to 9.';

?>

The condition is false. The output is...

Not equal to 9.



This is another example...

<?php

$num=24;

echo ($num == 12) ? 'Exactly twelve.' : ($num > 18)
     ? 'More than 12.' : 'Less than 12.';

?>

The output is...

More than 12.




There is a third alternative to evaluating a condition. The  switch()  function evaluates a variable as a condition. The case command compares the variable with a value. The  :  operator declares that the following statement is dependent on the condition. The break command stops the evaluation if the condition is true. The default command evaluates if all statements are false...

<?php

$num=7;

switch($num)
{case 5:
echo 'The number is five.';
break;
case 10:
echo 'The number is ten.';
break;
case 7:
echo 'The number is seven.';
break;
default:
echo 'default';}

?>

case 7 is true. The output is...

The number is seven.




SEO Vancouver, Washington
SEO Portland, Oregon
Website Design Vancouver, Washington