Вы находитесь на странице: 1из 38

Web Development: Dynamically Generated Content

Introduction to PHP

PHP Start and End Tags

You need to inform the PHP engine that you want to execute PHP code You can do this using special tags to mark start and end of PHP code

PHP Start and End Tags


Tag Style Standard tags Short tags ASP tags Script tags Start Tag <?php <? <% <script language=php> End Tag ?> ?> %> </script>

Standard and the script tags cab be guaranteed to work on any configuration

Creating a PHP Script


Load Notepad Type in the following PHP code

<?php phpinfo(); ?>

Save it as phpinfo.php

The echo Statement and print() Function

The echo statement and the print() function are used to output data Example
<? print(Hello World); ?> OR <? echo Hello World; ?>

Combining HTML and PHP


<html> <head> <title>A PHP script including HTML</title> </head> <body> <b> <?php echo "hello world"; ?> </b> </body> </html>

Adding Comments

Comments make it easier for other programmers to work with your code There two types of comments in PHP:

Single-line comments which begin with // or # //This is a single-line comment # This is also a single-line comment

Multi-line comments with /* and ending with */ /*This is a comment and will be ignored by PHP engine */

PHP LANGUAGE STRUCTURE

Variables

A variable consists of a name of your choosing preceded by a $ sign Variable names can include letters, numbers and the underscore character but can not include spaces A semi-colon is used to end a PHP statement
$name_of_student; $number123;

Variable names are case sensitive that means $sum and $Sum are treated as entirely different variable names

Variables

PHP is a loosely typed language This means that variable does not need to be declared before adding a value to it. PHP automatically converts the variable to the correct data type, depending on its value

Globals and Superglobals

Variables can be declared as global variables within a script of function If variable is declared as global in two different scripts then the two scripts are connected and There will only be one value shared by the two scripts

Globals and Superglobals


PHP has predefined variables called superglobals These variables are always present and their values are available to all your scripts

Examples of Superglobals

$_GET contains any variables provided to a script through the GET method $_POST contains any variables provided to a script through the POST method $_COOKIE contains any variables provided to a script through a cookie $_FILES contains any variables provided to a script through file uploads

Examples of Superglobals

$_SERVER contains information such as headers, file paths, and script locations $_ENV contains any variables provided to a script as part of the server environment $_REQUEST contains any variables provided to a script through any user input mechanism $_SESSION contains any variables that are currently registered in a session

Working with Strings


A string is a collection of characters, numbers, letters enclosed in quotes For example


Interactive Media Dynamic Websites

To make a string assign it a variable name


$name=Jimmy

To create a srting you can use single or double quotes A string can be printed using echo() or print()
echo $name Or echo HND Interactive Media

Concatenating Strings

To append a string at the end of another you can use the concatenation operator which is the period(.) $first_name=Jack; $last_name=Murray; $full_name= $first_name. $last_name;

String Functions

PHP has several functions to manipulate text(Strings) You would usually pass a variable by putting it in parentheses The functions can return a value in addition to performing tasks Functions do not start with a dollar sign and are immediately followed by parentheses

String Functions strlen()

The function strlen() returns the length of a string

<?php $the_var=HND Interactive Media; echo strlen($the_var);


?>

String Functions htmlspecialchars()

The function htmlspecialchars() takes a string and then converts special characters like >, to HTML code <?php $the_var=Audio & Video; echo htmlspecialchars($the_var);

?>

String Functions ucfirst()

The function ucfirst() changes the first character to uppercase <?php $the_var=interactive Media; echo ucfirst($the_var); ?>

String Functions ucwords()

The function ucwords() changes the first character of each word to uppercase <?php $the_var=website design and development; echo ucwords($the_var);

?>

String Functions trim()

The function trim() removes blank space from the beginning and end of string <?php $the_var= website design and development ; echo trim($the_var);

?>

String Functions strtolower()

The function strtolower() converts any uppercase letters to lowercase <?php $the_var=WEBSITE DESIGN AND DEVELOPMENT; echo strtolower($the_var);

?>

String Functions strtoupper()

The function strtoupper() converts any lowercase letters to uppercase <?php $the_var=website design and development; echo trim($the_var); ?>

PHP Data Types


Type Boolean Integer Float or Double String Array Description One of the values true or false A whole number A floating point number A collection of characters An ordered set of values

Resource
NULL

Reference to a third party resource e.g. database


An uninitialized variable

Operators and Expressions


Operators are symbols used to manipulate data A value that is operated on by operator is called operand A combination of operands and operators is called expressions

Assignment Operators

The assignment operator is =

$name=Dougie; $sum=45;

Working with Numbers

PHP has both integer and floating-point(decimal) number types Number values are never enclosed within quotes All numbers are presumed positive unless preceded by a minus(-)

Working with Numbers

Along with arithmetic operators you can use built-in functions to manipulate numbers e.g.

number_format() used to display numbers with specified number of decimal places number_format() has the following syntax number_format(variable, number of decimal places) number_format($total, 3);

round() is used to decimal numbers to nearest whole number round() has the following syntax round(variable) e.g. round($total);

Arithmetic Operators
Operator + / * % Name Addition Subtraction Division Multiplication Modulus

Combined Assignment Operators


Operator += -= /= *= %= Example $x+=5 $x-=5 $x/=5 $x*=5 $x%=5 Equivalent to $x=$x+5 $x=$x-5 $x=$x/5 $x=$x*5 $x=$x%5

.=

$x.= test

$x=$x. test

Automatically Incrementing and Decrementing


A variable can be incremented by one as follows $x=$x+1;

OR

$x+=1;

Automatically Incrementing and Decrementing

A variable can be decreased by 1 as follows:

$x=$x-1;

OR

$x-=1;

OR

$x--;

Comparison Operators
Operator == != === > >= Name Equivalence Non-equivalence Identical Greater than Greater than or equal to

<
<=

Less than
Less than or equal to

Constants

A constant is a value that does not change A constant is defined using function define() define (name_of_constant,41);

Predefined Constants

PHP automatically provides some built-in constants Examples

_FILE_ returns name of file PHP engine is currently reading _LINE_ returns the current line number of file PHP_VERSION returns version of PHP PHP_OS returns operating system

Single vs Double Quotes

In PHP values enclosed within single quotes will be treated literally whereas those within double quotes will be interpreted Lets say you have echo $test_var=Some Text; echo var is equal to $test_var; will print: var is equal to Some Text Whereas echo var is equal to $var; will print var is equal to $test_var

Special Characters

The following characters have special meaning when enclosed in double quotes
Code \ \ \\ \n \r \t Meaning Double quotes Single quotes Backslash New line Carriage return Tab

\$

Dollar sign