Using Php Variables
Php Variables are are used for storing values, such as strings, numbers। Once stored they can be called from anywhere within the script।
Note :Variables can not have spaces and only contain alpha-numeric characters and underscores. $myVar or $my_Var both is correct.
PHP Switch Statement
Php Switch statement is very useful, it works like if/else statement & reduce the complication of numerous conditions in the script.
Syntax :
switch (expression)
{
case 1
code .. //execute code if expression = 1
break;
case 2
code … //execute code if expression = 2
break;
default:
code…//execute code if expression not matched
}
Example :
$value = 6;
switch ($value)
{
case 1:
echo “Value 1″;
break;
case 2:
echo “Value 2″;
break;
case 3:
echo “Value 3″;
break;
default:
echo “Value is ” . $value . ” not matched!”;
}
?>
The while Statement
The while statement is useful, when you want to loop a code until it’s conditions are met.
Syntax :
while (condition)
code ….
Example :Output of this code is :
“);}
echo “Value is “. $val .”. condition not met!
“;
$val++;
}
?>
Value is 0. condition not met!
Value is 1. condition not met!
Value is 2. condition not met!
Value is 3. condition not met!
Value is 4. condition not met!
Value is 5. condition not met!
Value is 6. condition not met!
Value is 7. condition not met!
Value is 8. condition not met!
Value is 9. condition not met!
Value is 10. condition met!
Above code loops until the value is 10.
The do while Statement
The do/While repeat through the code, until the condition is met.
Syntax :
do { code … } while (condition);Example :
“; } while ($val<3);>Output of this code : Value is 1 Value is 2 Value is 3
PHP For Statement
PHP For statement is used when you want to loop the code for number of times,PHP functions re very useful, because once coded, it can be called from anywhere within the script or from outside. Note, code block must start with function word.
until condition is met.
Syntax :
for (initialization; condition; increment)
{
code …
}
Example :Output of this Php code is :
”;
}
?>
This is the text.
This is the text.
This is the text.
PHP Functions
Example 1 : Shows how to call function within the php Script.
function MyFirstEchoFunction()
{
echo “Hi this is the function called!
”;echo “And this is text created by function!”;
}
MyFirstEchoFunction()
?>
Another way of calling functions is using include()..
Php Include() lets you import any php or text file in your script.
Example 2 : Create a php file somthing like “test.php” and write this code:
test.php code :
function MyFirstEchoFunction()
{
echo “Hi this is the function called!
”;echo “And this is text created by function!”;
}
MyFirstEchoFunction()?>
Now create destination file, something like “call.php”
Call.php code :
include(”test.php”); //This is the target php file
MyFirstEchoFunction() //We just called the function coded in test.php?>
Isn’t this nice? The output of this file is :
Hi this is the function called!
And this is text created by function!
No comments:
Post a Comment