Conditional Statements:
• Like in C, in php too v have 4 types of conditional statements:
• if statement - use this statement to execute some code only if a specified condition is true.
• if...else statement - use this statement to execute some code if a condition is true and another code if the condition is false.
• if...elseif....else statement - use this statement to select one of several blocks of code to be executed.
• switch statement - use this statement to select one of many blocks of code to be executed.
• These statements r used with same syntax as used in C.
• Example of if statement:
<html>
<body>
<?php
$d=date("D");
if ($d=="Fri") echo "Have a nice weekend!";
?>
</body>
</html>
• Here date(“D”) is a built-in function which returns day and is stored in variable $d.
• Example of if...else statement:
<html>
<body>
<?php
$d=date("D");
if ($d=="Fri")
echo "Have a nice weekend!";
else
echo "Have a nice day!";
?>
</body>
</html>
• Example of if...elseif...else statement:
• <html>
<body>
<?php
$d=date("D");
if ($d=="Fri")
echo "Have a nice weekend!";
elseif ($d=="Sun")
echo "Have a nice Sunday!";
else
echo "Have a nice day!";
?>
</body>
</html>
• Example of switch statement:
switch (n)
{
case label1:
code to be executed if n=label1;
break;
case label2:
code to be executed if n=label2;
break;
default:
code to be executed if n is different from both label1 and label2;
}
• This is how it works: First we have a single expression n (most often a variable), that is evaluated once. The value of the expression is then compared with the values for each case in the structure. If there is a match, the block of code associated with that case is executed. Use break to prevent the code from running into the next case automatically. The default statement is used if no match is found.
PHP Functions:
• In PHP, there are more than 700 built-in functions.
• But here, I will show u how to create ur own functions. These r also similar to C.
• A function will be executed by a call to the function.
• You may call a function from anywhere within a page.
• Syntax to create a PHP function:
function functionName()
{
code to be executed;
}
• The function name can start with a letter or underscore (not a number)
• Example:
<html>
<body>
<?php
function writeName()
{
echo "Sakshi";
}
echo "My name is ";
writeName();
?>
</body>
</html>
• PHP function-Adding Parameters:
• Example:
<html>
<body>
<?php
function writeName($fname)
{
echo $fname . " <br />";
}
echo "My name is ";
writeName("Sakshi");
echo "My college name is ";
writeName("UIET");
echo "My blog's name is ";
writeName("Getting Started With PHP");
?>
</body>
</html>
• PHP Functions-Returns Values:
• Example:
<html>
<body>
<?php
function add($x,$y)
{
$total=$x+$y;
return $total;
}
echo "1 + 16 = " . add(1,16);
?>
</body>
</html>
• Note-If a function needs to use a variable that is defined in the main body of the program, it must reference it using the "global” keyword. You may notice that this is a little bit different from the C language in that global variables in C are automatically available to functions unless specifically overridden by a local definition. This can cause some problems in that people may inadvertently change a global variable. In PHP global variables must be declared global inside a function if they are going to be used in that function.
• Example:
<?php
$a = 1;
$b = 2;
function Sum()
{
global $a, $b;
$b = $a + $b;
}
Sum();
echo $b;
?>
function writeName($fname)
ReplyDelete{
echo $fname . " Refsnes.
";
}
hey wht does ....this thing " Refsnes.
"....
stand here for????
Hey dis is added by mistake...
ReplyDeleteactually it was surname...hehe :)
dnt wry abt dat..
oh!!!oh!!!
ReplyDeleteit's ok den...
:)