Chap1 PHP Basics

You might also like

Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 16

Website Development

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

Current version PHP 5 using Zend 2 engine (from authors names)

Very popular web server scripting application


Cross platform & open source Built for web server interaction

How does it work?


PHP scripts are called via an incoming HTTP request from a web client
Server passes the information from the request to PHP

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

More about PHP


PHP is a powerful server-side scripting language for creating dynamic and interactive websites. PHP is the widely-used, free, and efficient alternative to competitors such as Microsoft's ASP. PHP is perfectly suited for Web development and can be embedded directly into the HTML code. The PHP syntax is very similar to Perl and C. PHP is often used together with Apache (web server) on various operating systems. It also supports ISAPI and can be used with Microsoft's IIS on Windows. A PHP file may contain text, HTML tags and scripts. Scripts in a PHP file are executed on the server. PHP is FREE to download from the official PHP resource: www.php.net PHP is easy to learn and runs efficiently on the server side

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

Useful for diagnostic work & administration


May tell you why something isn't available on your server
print(phpinfo());

What does a PHP script look like?


A text file (extension .php)
Contains a mixture of content and programming instructions Server runs the script and processes delimited blocks of PHP code Text (including HTML) outside of the script delimiters is passed directly to the output e.g.

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

Programming with PHP


PHP uses common language structures
Similar syntax to Perl, JavaScript etc

PHP is interpreted (not compiled)


No special tools needed

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

Variable names are case-sensitive

PHP is a Loosely Typed Language


In PHP a variable does not need to be declared before being set. In the example above, you see that you do not have to tell PHP which data type the variable is. PHP automatically converts the variable to the correct data type, depending on how they are set. In a strongly typed programming language, you have to declare (define) the type and name of the variable before using it. In PHP the variable is declared automatically when you use it.

PHP supports eight primitive types


Four scalar types: boolean integer float (floating-point number, 'double') string Two compound types: array object And finally two special types: resource NULL

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

5>8 returns false


5<8 returns true 5>=8 returns false

<=

is less than or equal to

5<=8 returns true

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

You might also like