PHP Tutorial (Basic)

27
PHP Tutorial (Basic) Prepared By: Mohamad Marwan Hadid B. Mohamad Salim

Transcript of PHP Tutorial (Basic)

Page 1: PHP Tutorial (Basic)

PHP Tutorial (Basic)Prepared By:

Mohamad Marwan Hadid B.Mohamad Salim

Page 2: PHP Tutorial (Basic)

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)

Page 3: PHP Tutorial (Basic)

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

Page 4: PHP Tutorial (Basic)

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"

Page 5: PHP Tutorial (Basic)

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)

Page 6: PHP Tutorial (Basic)

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 <? ?>

Page 7: PHP Tutorial (Basic)

Simple Example

<html>

<body> <?php echo "Hello World"; ?> </body>

</html>

Page 8: PHP Tutorial (Basic)

Comments in PHPIn 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 */ ?>

Page 9: PHP Tutorial (Basic)

PHP VariablesA variable is used to store information.

text stringsnumbers Arrays

Examples:A variable containing a string:

<?php $txt="Hello World!"; ?>

A variable containing a number: <?php

$x=16;

?>

Page 10: PHP Tutorial (Basic)

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)

Page 11: PHP Tutorial (Basic)

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!

Page 12: PHP Tutorial (Basic)

The strlen() function The strlen() function is used to return the

length of a string. Example:

<?php echo strlen("Hello world!"); ?>

Output:12

Page 13: PHP Tutorial (Basic)

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

Page 14: PHP Tutorial (Basic)

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

Page 15: PHP Tutorial (Basic)

Arithmetic Operators

Page 16: PHP Tutorial (Basic)

Assignment Operators

Page 17: PHP Tutorial (Basic)

Comparison Operators

Page 18: PHP Tutorial (Basic)

Logical Operators

Page 19: PHP Tutorial (Basic)

Tutorial ExercisePHP 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 = 310 * 7 = 7010 / 7 = 1.428571428571410 % 7 = 3

Use numbers only in the above variable assignments, not in the echo statements. You will need a third variable as well.

Page 20: PHP Tutorial (Basic)

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 ";

?>

Page 21: PHP Tutorial (Basic)

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

Page 22: PHP Tutorial (Basic)

Example: if..else statement <html> <body> <?php $d=date("D"); if ($d=="Fri") echo "Have a nice weekend!"; elseecho "Have a nice day!"; ?> </body> </html>

Page 23: PHP Tutorial (Basic)

Continue.. If..elseIf 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!"; } ?>

Page 24: PHP Tutorial (Basic)

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"; }?>

Page 25: PHP Tutorial (Basic)

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.

Page 26: PHP Tutorial (Basic)

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.'; 

Page 27: PHP Tutorial (Basic)

That’s all for today. Thank You