You are on page 1of 11

PHP Lecture Notes

Introduction
PHP stands for Hypertext Preprocessor Server Side language Using scripts on your website allows you to add many new interactive features like feedback forms, guestbooks, message boards, counter, etc. You can access database and files. With these things on your website you will find that it gives a more professional image. Being web-oriented, PHP also contains all the functions youll need to do things on the Internet. There are functions for connecting to remote webserver, sending email and urlencoding strings to protect special characters. The script is not visible to the user, only the output. PHP must be installed on the webserver. PHP is declared between <?php?> or <script language=php></script> Comments are // or /*.*/ Discuss hello.php

Variables
PHP is not a strongly typed language. The same variable can hold strings or numbers. They dont have to be declared, just use them or reuse them: $welcome=Hello and welcome to my website.; $numvisitor=27; $welcome=83; Numbers All the rule about precedence apply Strings To assign a string to a variable, you must use quotes. Double quotes allow for variable substitution and escape sequences, single quotes do not. Ex. $first_name=Mimi; $greeting1 = Hello, my first name is $first_name; echo $greeting1; $last_name=Opkins; $greeting2=Hello, my last name is $last_name.; echo $greeting2; Output: Hello, my first name is Mimi. Hello, my last name is $last_name. Ex. echo Mimi\nOpkins; Output: Mimi Opkins In the source code only, not a break on the page. To break lines on the page you still need a <BR> tag. To insert or or \ you need to use an escape character (\) Ex. Mimi \the teacher\ Opkins Output: Mimi the teacher Opkins Or you can use but you wont be able to use variable substitution If you want to concatenate two strings use the . operator. Ex. $full_name=$first_name . . $last_name

Output: Mimi Opkins Arrays You can create arrays use integer subscripts or you can create associate arrays. Ex. $name[0] = Mimi; $name[1]=Opkins; echo $name[0] . . $name[1]; Output: Mimi Opkins Associative arrays use strings for substrings instead of integers. Ex. $personal_data[firstname]=Mimi; $personal_data[nickname]=teacher; $personal_data[lastname]=Opkins; $personal_data[age]=29; echo Hello . $personal_data[firstname] . ! We know that you are . $personal_data[age] . years old.; Output: Hello Mimi! We know that you are 29 years old.

Control Statements
IF statement Normal if..then..else statement Ex. if ($a<5) { $b=$a+10; } else { $b=$a*2; } Operator == != < <= > >= && and || or Meaning Is Equal to Not Equal to Is less than Is less than or equal to Is Greater than Is Greater than or equal to And Or

As soon as the true or falsity of a Boolean expression can be determined, the comparisons stop. EX. mysql_connect() or die(Could not connect to the database!); Output: The function mysql_connect() tries to make a connection to a MySQl database. If it returns true if it was able to make a connection and PHP skips the next statement. The second statement makes PHP stop with an error message, if the first statement fails. For loop Normal for loop: for (starting value;ending value;increment) Ex. for ($i=0; $i<10; $i++){ echo $i; }

While Loop Normal while loop while (something is true) { some statements } Ex. $i=0; while ($i<10){ echo $i; $i++; }

Functions
User-Defined Functions Allow for code reuse Ex. function checkten($arg) { if ($arg<10) { echo $arg is less than 10; } else { echo $arg is greater than or equal to 10; } } foo(15); You can also return values from a function. Ex. function difften($arg) { return $arg-10; } echo The difference of 14 and 10 is difften(14).

Pre-Defined Functions PHP has a vast library of pre-defined functions. A reference manual can be found at: http://www.php.net/manual/en/funcref.php Discuss today.php Discuss email.php Environment Variables Discuss phpinfo.php Discuss browsertest.php Discuss cookie.php Some of the more useful String functions are:

PHP String functions explode Split a string by string implode join array elements with a string join join array elements with a string (same as implode) ltrim strip whitespace from the beginning of a string number_format format a number with grouped thousands rtrim strip whitespace from the end of a string sprintf return a formatted string sscanf parses input from a string according to a format strchr find the first occurrence of a character strcmp string comparison strlen get string length strpos find the position of fist occurrence of a string strstr find the first occurrence of a string strtok tokenize string strtolower make a string lowercase strtoupper make a string uppercase trim strip whitespace from the beginning and end of a string

Working With Forms


Form data can be retrieved through either: $variable_name=$HTTP_GET_VARS[variable] or $variable_name=$HTTP_POST_VARS[variable] or just by $variable where variable is the string used in the name attribute of the html tag Discuss form.php/action.php Discuss emailform.html/emailform.php Discuss icecream.html/icecream.php

File I/ O
fopen() takes two arguments, file name and mode and returns a file pointer. A file pointer provides information about the file and is used as a reference. The file name is the full or relative path to the file you want to create or open and mode can be any of the following: File Mode Description r opens the existing file in order to read data from it. The file pointer is placed at the beginning of the file, before any data r+ opens the existing file for reading or writing. The file pointer is placed at the beginning of the file, before any data opens a file only for writing. If a file with that name does not exist, attempts to create a new file. If the file does exist, deletes all existing contents and places the file pointer at the beginning of the file opens a file for reading and writing. If a file with that name does not exist, attempts to create a new file. If the file does exist, deletes all existing contents and places the file pointer at the beginning of the file opens a file only for writing. If a file with that name does not exist, attempts to create a new file. If the file does exist, places the file pointer at the end of the file, after all other data opens a file for reading and writing. If a file with that name does not exist, attempts to create a new file. If the file does exist, places the file pointer at the end of the file, after all other data

w+

a+

Ex. $newfile=fopen(/usr/local/apache/htdocs/mydata.txt,a+); fclose() closes a file Ex: fclose($newfile); fwrite() or fputs() writes data to an open file: fwrite(file,data) Ex. fwrite($newfile,This is a new file.); fread() reads data from an open file: fread(file,length) To read the entire file use the filesize(filename) function Ex. fread($newfile,filesize(/usr/local/apache/htdocs/mydata.txt))

Discuss readwrite.php

You might also like