Chap1 PHP Basics
Chap1 PHP Basics
PHP Roundup
PHP Basics
Introduction to PHP How PHP works The PHP.ini File Basic PHP Syntax Variables and Expressions PHP Operators
What is PHP?
PHP Hypertext Pre-processor
Project started in 1995 by Rasmus Lerdorf as "Personal Home Page Tools"
Taken on by Zeev Suraski & Andi Gutmans
The PHP script runs and passes web output back via the web server as the HTTP response
Extra functionality can include file writing, db queries, emails etc.
HTTP
HTML
HTML
Client
Web server
PHP Platforms
PHP can be installed on any web server: Apache, IIS, Netscape, etc PHP can be installed on any OS: Unix, Linux, Windows, MacOS, etc LAMP Stack Linux, Apache, MySQL, PHP 30% of web servers on Internet have PHP installed
Architecture
What is a PHP File? PHP files may 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"
phpinfo()
Exact makeup of PHP environment depends on local setup & configuration Built-in function phpinfo() will return the current set up
Not for use in scripts
<html> <head><title>Simple PHP</title></head> <body> <? for ($i=0; $i<10; $i++) { print("<p>Hello World!! </p>"); } ?> </body> </html>
Code inside <? ... ?> delimiters (or <?php ... ?>)
Each programming statement must <? end with a $name="Paul"; semi-colon e.g. print("<p>$name</p>");
?>
Variables
Placeholders to store data values
Strings (text), numbers, Boolean (true/false) etc No need to declare type or name before use
All variable names start with a dollar sign $
$myname = "Paul"; $myAge = 21; String values assigned in quotes
PHP Operators
Operators are used to operate on values. PHP Operators This section lists the different operators used in PHP. Arithmetic Operators
Operator + * / % Description Addition Subtraction Multiplication Division Modulus (division remainder) Example x=2 x+2 x=2 5-x x=4 x*5 15/5 5/2 5%2 10%8 10%2 x=5 x++ x=5 x-Result 4 3 20 3 2.5 1 2 0 x=6 x=4
++ --
Increment Decrement
PHP Operators
Assignment Operators
Operator = += -= *= /= .= %= Example x=y x+=y x-=y x*=y x/=y x.=y x%=y Is The Same As x=y x=x+y x=x-y x=x*y x=x/y x=x.y x=x%y
Comparison Operators
Operator
== != Description is equal to is not equal Example 5==8 returns false 5!=8 returns true
>
< >=
is greater than
is less than is greater than or equal to
<=
PHP Operators
Logical Operators
Operator &&
Description and
Example x=6 y=3 (x < 10 && y > 1) returns true x=6 y=3 (x==5 || y==5) returns false x=6 y=3 !(x==y) returns true
||
or
not