PHP Tutorial (Basic) : Prepared By: Mohamad Marwan Hadid B. Mohamad Salim
PHP Tutorial (Basic) : Prepared By: Mohamad Marwan Hadid B. Mohamad Salim
PHP Tutorial (Basic) : Prepared By: Mohamad Marwan Hadid B. Mohamad Salim
Prepared By:
Mohamad Marwan Hadid B.
Mohamad Salim
Introduction
Pre-requisites
Basic understanding of HTML and JAVASCRIPT
What is PHP?
Stands for PHP: Hypertext Preprocessor
Server-side scripting language, like ASP
Executed on the server
Supports many databases (MySQL, Informix, Oracle,
Sybase, Solid, PostgreSQL, Generic ODBC, etc.)
Open source software (Free to download and use)
PHP Installation
What do you need?
Most people would prefer to install all-in-one solution:
WampServer -> for Windows platform Includes : -
Apache 2.2.11 - MySQL 5.1.36 - PHP 5.3.0
<body>
<?php echo "Hello World"; ?>
</body>
</html>
Comments in PHP
In PHP, we use // to make a single-line comment or /*
and */ to make a large comment block.
Example :
<?php
//This is a comment
/* This is a comment block */
?>
PHP Variables
A variable is used to store information.
text strings
numbers
Arrays
Examples:
A variable containing a string:
<?php
$txt="Hello World!";
?>
A variable containing a number:
<?php
$x=16;
?>
Naming Rules for Variables
Must start with a letter or an underscore "_"
?>
PHP If...Else Statements
Conditional statements are used to perform different
actions based on different conditions.
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
Example: 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>
Continue.. If..else
If more than one line should be executed if a condition is
true/false, the lines should be enclosed within curly braces:
Example:
<?php
$d=date("D");
if ($d=="Fri")
{
echo "Hello!<br />";
echo "Have a nice weekend!";
echo "See you on Monday!";
}
?>
PHP Switch Statement
Conditional statements are used to perform different actions based on different conditions.
Example:
<?php
switch ($x) {
case 1:
echo "Number 1";
break;
case 2:
echo "Number 2";
break;
case 3:
echo "Number 3";
break;
default:
echo "No number between 1 and 3";
}
?>
Tutorial Exercise
A product that you are selling is discounted depending on the number purchased. Try out the following “nested if-
statement” code that sets the price for the product, given a number of different quantities:
// -- Price depends on quantity
$quantity = <choose a quanity>;
if ($quantity > 0) {
$price = 100;
if ($quantity >= 10) {
$price = 50;
if ($quantity >= 25) {
$price = 35;
}
}
}
else
$price = "no purchase";
echo $quantity . ' products will cost ' . $price . ' each.';
Rewrite the above program using if-elseif-else statements to remove the nested if-statements. Hint: don’t just replace
“if” statements with “if-else” in the above. You will have to move the end }’s too! Consider example quantities of 1, 10,
and 25 and make sure that the correct price is set for each.
Answer for Tutorial Exercise
// -- Price depends on quantity
$quantity = <choose a quanity>;
echo $quantity . ' products will cost RM ' . $price . ' each.';
That’s all for today.
Thank You