(c) Manzur Ashraf, Short course, KFUPM 2004 --- PHP & MySQL
1
Basic PHP
Class 2
(c) Manzur Ashraf, Short course, KFUPM 2004 --- PHP & MySQL
2
Declaring PHP
The three different forms are as follows:
<?PHP Code In Here?>
<?phpPHP Code In Herephp?>
<script language="php">PHP Code In Here</script>
(c) Manzur Ashraf, Short course, KFUPM 2004 --- PHP & MySQL
3
Your First Script
<?phpinfo();?>
Testing Your Script ?
(c) Manzur Ashraf, Short course, KFUPM 2004 --- PHP & MySQL
4
Displaying Information & Variables
Printing Text :<?print("Hello world!");?>
Variables:$welcome_text = "Hello and welcome to my website."; $user_id = 987
Example: <?
$welcome_text = "Hello and welcome to my website.";print($welcome_text);?>
(c) Manzur Ashraf, Short course, KFUPM 2004 --- PHP & MySQL
5
Formatting Your Text
<font face="Arial" color="#FF0000"></font>
PHP: print("<font face=\"Arial\"color\"#FF0000\">Hello and welcome
to my website.</font>");
(c) Manzur Ashraf, Short course, KFUPM 2004 --- PHP & MySQL
6
IF Statements
IF (something == something else) (THEN Statement} else {ELSE Statement}-----------------------
if ($username == "webmaster") {echo "Please enter your password below";}
_______________________ if ($username == "webmaster") {
echo "Please enter your password below";} else {echo "We are sorry but you are not a recognised user";}
(c) Manzur Ashraf, Short course, KFUPM 2004 --- PHP & MySQL
7
Other comparisons
if ($enteredpass == $password)----------------------------------------------------------
if ($age < "13")---------------------------------------------------------
if ($date > $finished)-----------------------------------------------------------
You can also check for multiple tests in one IF statement. For instance, if you have a form and you want to check if any of the fields were left blank you could use:
if ($name == "" || $email == "" || $password == "") {echo "Please fill in all the fields";}
(c) Manzur Ashraf, Short course, KFUPM 2004 --- PHP & MySQL
8
The WHILE Loop
$times = 5;$x = 0;while ($x < $times) {echo "Hello World";++$x; /* It means $x=$x+1; */
} --------------------------
Using $x : $number = 1000;
$current = 0;while ($current < $number) {++$current;echo "$current<br>";}
(c) Manzur Ashraf, Short course, KFUPM 2004 --- PHP & MySQL
9
Arrays
> Setting Up An Array
Setting up an array is slightly different to setting up a normal variable. In this example I will set up an array with 5 names in it:
$names[0] = 'John';$names[1] = 'Paul';$names[2] = 'Steven';$names[3] = 'George';$names[4] = 'David';OR $names=array(“John”,”Paul”,”Steven”);
> Reading From An Array
echo "The third name is $names[2]";
(c) Manzur Ashraf, Short course, KFUPM 2004 --- PHP & MySQL
10
Using Arrays And Loops
if I wanted to print out the following list of names:
Name 1 is JohnName 2 is PaulName 3 is StevenName 4 is GeorgeName 5 is David
I could use the following code:
$number = 5;$x = 0;while ($x < $number) {$namenumber = $x + 1;echo "Name $namenumber is $names[$x]<br>";++$x}
(c) Manzur Ashraf, Short course, KFUPM 2004 --- PHP & MySQL
11
PHP With Forms
Setting Up Your Form <input type="text" name="thebox" value="Your Name"> <textarea name="message">Please write your message here.</textarea><input type="submit" value="Submit">
All the elements for your form must be enclosed in the <form> tags. They are used as follows:
<form action="process.php" method="post">Form elements and formatting etc.</form>
(c) Manzur Ashraf, Short course, KFUPM 2004 --- PHP & MySQL
12
Getting The Form Information
There are basically two different methods of getting the data into PHP, which depend on how they were submitted. There are two submission methods, GET and POST, which can both be used by forms. The difference between the two is that using GET, the variables and data will be shown in the page address, but using POST it is invisible. The benefit of GET, though is that you can submit information to the script without a form, by simply editing the URL:
yourpage.php?user=davidcould show David's page and:yourpage.php?user=tomcould show Tom's page, using the same script.
(c) Manzur Ashraf, Short course, KFUPM 2004 --- PHP & MySQL
13
Contd..
It is also possible to pass more than one piece of information to the script using this system by separating them with the & symbol:
yourpage.php?user=david&referrer=gowansnet&area=6
To get a variable which has been sent to a script using the POST method you use the following code:$variablename=$_POST['variable'];
Similarly, if you are using the GET method you should use the form:$variablename=$_GET['variable'];
(c) Manzur Ashraf, Short course, KFUPM 2004 --- PHP & MySQL
14
Example (Creating The Form To Mail Script )
create this form for your HTML page:
<form action="mail.php" method="post">Your Name: <input type="text" name="name"><br>E-mail: <input type="text" name = "email"><br><br>Comments<br><textarea name="comments"></textarea><br><br><input type="submit" value="Submit"></form>
(c) Manzur Ashraf, Short course, KFUPM 2004 --- PHP & MySQL
15
Contd..
<?
$name=$_POST['name'];$email=$_POST['email'];$comments=$_POST['comments'];
$to="[email protected]";
$message="$name just filled in your comments form. They said:\n$comments\n\nTheir e-mail address was: $email";if(mail($to,"Comments From Your Site",$message,"From: $email\n")) {echo "Thanks for your comments.";} else {echo "There was a problem sending the mail. Please check that you filled in the form correctly.";}?>
(c) Manzur Ashraf, Short course, KFUPM 2004 --- PHP & MySQL
16
E-mail With PHP (Optional to basic level)
The Mail Commandmail($to,$subject,$body,$headers);
Sending An E-mail$to = "[email protected]";$subject = "PHP Is Great";$body = "PHP is one of the best scripting languages around";$headers = "From: [email protected]\n";mail($to,$subject,$body,$headers);echo "Mail sent to $to";
(c) Manzur Ashraf, Short course, KFUPM 2004 --- PHP & MySQL
17
Contd..
Mail Without Variablesmail("[email protected]","PHP Is Great","PHP is one of the best scripting languages around","From: [email protected]\n");
Error Control if(mail($to,$subject,$body,$headers)) {
echo "An e-mail was sent to $to with the subject: $subject";} else {echo "There was a problem sending the mail. Check your code and make sure that the e-mail address $to is valid";}
(c) Manzur Ashraf, Short course, KFUPM 2004 --- PHP & MySQL
18
File handling
Opening a File :
$filename = 'c:\file.txt';
$fp = fopen($filename, "r");
Reading Info from a File:
$contents = fread($fp, filesize($filename));
fclose($fp);
/* it reads the entire file in */
(c) Manzur Ashraf, Short course, KFUPM 2004 --- PHP & MySQL
19
Contd..
Let us assume C:file.txt contains:
line1
line2
line3
line4
Line5 So now $contents would contain:
$contents = "line1\n line2\n line3\n line4\n line5";
(c) Manzur Ashraf, Short course, KFUPM 2004 --- PHP & MySQL
20
Contd..
Writing to a File:$filename = 'c:\file.txt';$fp = fopen($filename, "a");$string = "\nline5";$write = fputs($fp, $string);fclose($fp); Firstly we open the file. Notice the "a" parameter. That means
"open the file for writing only, and place the file pointer at the end of the file". So now PHP opens myfile.txt for writing and positions the file pointer at the end of the line.
We then use fputs() to write to the file. We define our string in $string which i put "\nline6. The \n bit means start a new line, and the print out "line 6". We then close the file.
(c) Manzur Ashraf, Short course, KFUPM 2004 --- PHP & MySQL
21
Modes
'r' - Open for reading only; place the file pointer at the beginning of the file.
'r+' - Open for reading and writing; place the file pointer at the beginning of the file.
'w' - Open for writing only; place the file pointer at the beginning of the file and truncate the file to zero length. If the file does not exist, attempt to create it.
'w+' - Open for reading and writing; place the file pointer at the beginning of the file and truncate the file to zero length. If the file does not exist, attempt to create it.
'a' - Open for writing only; place the file pointer at the end of the file. If the file does not exist, attempt to create it.
'a+' - Open for reading and writing; place the file pointer at the end of the file. If the file does not exist, attempt to create it.
(c) Manzur Ashraf, Short course, KFUPM 2004 --- PHP & MySQL
22
Questions?