PHP Tutorial (Basic)
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
Download from http://www.wampserver.com/en/
http://lamphowto.com/ -> for Linux platform
What is a PHP File?
PHP files can contain text, HTML tags and scripts
PHP files are returned to the browser as plain HTML
PHP files have a file extension of ".php", ".php3", or
".phtml"
What is MySQL?
Database server (Data Storage)
Ideal for both small and large applications
Supports standard SQL
Compiles on a number of platforms (Linux,
Windows, etc)
Basic PHP Syntax
A PHP scripting block always starts with <?php and
ends with ?>
A PHP scripting block can be placed anywhere in
the document.
On servers with shorthand support enabled you can
start a scripting block with <? and end with ?>
For maximum compatibility;
use the standard form <?php ?>rather than the
shorthand form <? ?>
Simple Example
<html>
<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 "_"
Can only contain alpha-numeric characters and
underscores (a-z, A-Z, 0-9, and _ )
Should not contain spaces. If a variable name is more
than one word, it should be separated with an
underscore ($my_string), or with capitalization
($myString)
PHP String Variables
It is used to store and manipulate text.
Example:
<?php
$txt1="Hello World!";
$txt2="What a nice day!";
echo $txt1 . " " . $txt2;
?>
Output:
Hello World! What a nice day!
The strlen() function
The strlen() function is used to return the length of a
string.
Example:
<?php
echo strlen("Hello world!");
?>
Output:
12
The strpos() function
The strpos() function is used to search for character within a
string.
If a match is found, this function will return the position of the
first match. If no match is found, it will return FALSE.
Example:
<?php
echo strpos("Hello world!","world");
?>
Output:
6
Complete reference of all string functions visit
http://www.w3schools.com/php/php_ref_string.asp
PHP Operators
Operators are used to operate on values.
4 General Types of Operators
1. Arithmetic Operators
2. Assignment Operators
3. Comparison Operators
4. Logical Operators
Arithmetic Operators
Assignment Operators
Comparison Operators
Logical Operators
Tutorial Exercise
PHP includes all the standard arithmetic operators. For this PHP
exercise, you will use them along with variables to print equations to
the browser. In your script, create the following variables:
$x=10;
$y=7;
Write code to print out the following:
10 + 7 = 17
10 - 7 = 3
10 * 7 = 70
10 / 7 = 1.4285714285714
10 % 7 = 3
Use numbers only in the above variable assignments, not in the echo
statements. You will need a third variable as well.
Answer for Tutorial Exercise
<?php
$x=10;
$y=7;
$a = $x + $y;
echo " $x + $y = $a <br>";
$a = $x - $y;
echo " $x - $y = $a <br>";
$a = $x * $y;
echo " $x * $y = $a <br>";
$a = $x / $y;
echo " $x / $y = $a <br>";
$a = $x % $y;
echo " $x % $y = $a ";
?>
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>;
if ($quantity >= 25)
$price = 35;
elseif ($quantity >= 10)
$price = 50;
elseif ($quantity > 0)
$price = 100;
else
$price = "no purchase";
echo $quantity . ' products will cost RM ' . $price . ' each.';
That’s all for today.
Thank You