Academic Poster for Download:
https://drive.google.com/file/d/0B0dgI5sYlhnkTmRGeWxFVklhZTg/view?usp=sharing
Saturday, December 3, 2016
Tuesday, September 6, 2016
Condition Statement in PHP
Program is a set of instructions for computer to execute. It is important to tell the computer when to execute an instruction and when not to. Thus, the condition statement has become a very important code in programming language. A proper planned condition statement may make the computer execute "WISELY". An artificial intelligent nothing but a bunch of condition statement to help computer to decide the next move.Type of condition statement in PHP
- If (condition) { ….. }
- If (condition) { ….. } else { ….. }
- If (condition) { ….. } else if(condition) { ….. } else { ….. }
- switch (var) { case x: …..; break; case y: …..; break; }
If (condition) { ….. }
This is the simplest form of conditional statement. This statement will check whether the condition is TRUE to execute block of codes within the curly brace.
<?php
$age = $_POST['age'];
if($age>18){
echo "You are adult!";
}
}
?>
If (condition) { ….. } else { ….. }
This condition statement contain two possible states (TRUE or FALSE). This statement will check whether the condition is TRUE to execute the first block of code and otherwise execute the second block of code in the else block.
<?php
$age = $_POST['age'];
if($age>18){
echo "You are adult!";
}else{
echo "You are just a kid";
}
}else{
echo "You are just a kid";
}
?>
If (condition) { ….. } else if(condition) { ….. } else { ….. }
This condition statement contain multiple possible states. If the first condition is true, the first block of codes will be executed Otherwise, if second condition is met, the second block of codes will be executed.
<?php
$age = $_POST['age'];
if($age>0 and $age<=1){
echo "You are Infant!";
}else if($age>1 and $age<=3){
echo "You are toddler";
}else if($age>3 and $age<=18){
echo "You are child";
}else if($age>18){
echo "You are adult";
}
}else if($age>1 and $age<=3){
echo "You are toddler";
}else if($age>3 and $age<=18){
echo "You are child";
}else if($age>18){
echo "You are adult";
}
?>
switch (var) { case x: …..; break; case y: …..; break; }
This condition statement contain multiple possible states. If any of this condition is true, the case block of that particular code will be execute.
<?php
$menuOption = $_POST['option'];
switch($menuOption){
case 1: echo 'Show Record';
break;
case 2: echo 'Add Record';
break;
case 3: echo 'Delete Record';
break;
case 4: echo 'Update Record';
break;
default: echo 'Invalid Option';
break;
}
?>
Form and PHP code
Methods to send Form data
There are two methods to send user inputs to script:
POST method is a safer method as the inputs will be send by attaching to the trail of the HTTP protocol.
- GET
- POST
POST method is a safer method as the inputs will be send by attaching to the trail of the HTTP protocol.
Data passed from FORM
$_GET is an array contains values passed using GET method. Syntax: $_GET [name] where name is the name attribute of form element$_POST is an array contains values passed using POST method. Syntax: $_POST [name] where name is the name attribute of form element
Options to handling FORM with PHP:
- Separated form and PHP code.
- PHP code and form in a single PHP file
Separated form and PHP code.
The easiest way to handle user input is used separated form and script. In this case, you need to create a form in HTML file. And, in the action attribute specify the PHP file that handle input from this file.
form.html
<form action="process.php" method="get">
Age : <input type="text" name="age" />
<input type="submit" name="submit" />
</form>
process.php
<?php
echo $_GET['age'];
?>
form.html
<form action="process.php" method="get">
Age : <input type="text" name="age" />
<input type="submit" name="submit" />
</form>
process.php
<?php
echo $_GET['age'];
?>
PHP code and form in a single PHP file
In this option, the PHP file consists of two roles: display form and process the input. PHP file has to determined when to display a form and when to process the input. It is very critical as the PHP code cannot process the task if there is no data from the form. Thus, we need to check whether the user has submitted the form. If the user did not submit form, display form for user input. If the user has filled in the form and pressed the submit button, Then, PHP will process the inputs from user.
registration.php
<?php
if(isset($_POST['submit'])){
// process the task as the submit button is pressed
}else{
// Display the form if the submit is not detected.
?>
<form action="process.php" method="get">
Age : <input type="text" name="age" />
<input type="submit" name="submit" />
</form>
<?php
}
?>
registration.php
<?php
if(isset($_POST['submit'])){
// process the task as the submit button is pressed
}else{
// Display the form if the submit is not detected.
?>
<form action="process.php" method="get">
Age : <input type="text" name="age" />
<input type="submit" name="submit" />
</form>
<?php
}
?>
Fundamental of PHP
- PHP code must be enclosed within a block: <?php ……. ?>
- Multiple PHP blocks are allowed in a single document.
- HTML tags can be used within PHP document.
- PHP file can be only executed in web server. It cannot run as a normal HTML file in the local file system.
- End each statement with a semicolon (;)
- Variables start with $ and it is scalable type (no need to declare before used)
- Variable name cannot start with number and it can be any alphanumeric value after the first character.
- echo is function that can print output to browser
- Form is an user interface that accept users input for server to process.
- Form must contains two attributes: action and method. Action attribute specified the script or program that will handle the input. Method is the sending method used (GET or POST). And, a submit button that will do the sending according to these two attributes.
PHP code must be enclosed within a block: <?php ……. ?>
HTML tags do not work within the PHP block. It must use echo function to output them to the browser.
<?php
echo "<H1>Heading Text</H1>";
echo "<P>Paragraph</p>";
?>
<H2>Sub Heading Text</H2>
<p>Paragraph in Sub section</p>
<?php
echo "<H1>Heading Text</H1>";
echo "<P>Paragraph</p>";
?>
<H2>Sub Heading Text 1</H2>
<p>Paragraph in Sub section</p>
<?php
echo "<H2>Sub Heading Text 2</H2>";
echo "<p>Paragraph in Sub section</p>";
?>
<?php
echo "<H1>Heading Text</H1>";
echo "<P>Paragraph</p>";
?>
<H2>Sub Heading Text</H2>
<p>Paragraph in Sub section</p>
Multiple PHP blocks are allowed in a single document.
HTML tags can be used within PHP document.
End each statement with a semicolon (;)
<?php
echo "<H1>Heading Text</H1>";
echo "<P>Paragraph</p>";
?>
<H2>Sub Heading Text 1</H2>
<p>Paragraph in Sub section</p>
<?php
echo "<H2>Sub Heading Text 2</H2>";
echo "<p>Paragraph in Sub section</p>";
?>
Variables start with $ and it is scalable type (no need to declare before used)
Variable name cannot start with number and it can be any alphanumeric value after the first character.
echo is function that can print output to browser
<?php
$B2B = true;
// $1MDB = "News"; <- Invalid variable start with number
$age = 25;
$name = "Johnson";
$years = 3.5;
echo "Name : ".$name."<br />Age : ".$age;
Form is an user interface that accept users input for server to process.
Form must contains two attributes: action and method. Action attribute specified the script or program that will handle the input. Method is the sending method used (GET or POST). And, a submit button that will do the sending according to these two attributes.
<form action="process.php" method="get">
Age : <input type="text" name="age" />
<input type="submit" name="submit" />
</form>
Subscribe to:
Posts (Atom)