Working With PHP. David Lash Module 1 Getting Started. Objectives. To understand what PHP is What is HTML How PHP compares with CGI Getting Started with PHP Your first script PHP syntax and embedding in HTML Finding out about your PHP configuration Using PHP variables
Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.
Working With PHP
You need to
People seem to forget this
The file is just
retrieved no code is executed on server
HTML tags interpreted on client by browser.
You need to
Source netscrat http://www.netscraft.com
Advantages of Using PHP to enhance Web pages:
On a UNIX server this is a fork operation
Note this can be shell script, perl script, C program or whatever.
To develop and publish PHP scripts you need:
Its worth noting:
There are a couple good IDEs for PHP.
They basically allow you create PHP scripts on PC, syntax check them, see output, and then copy to Web server
FTP or telnet
You can use a number of different editors to create your PHP script files.
print ("A simple initial script");
Its worth noting:
Some web hosts allow direct access to servers with SSH telnet. Others only allow you to use FTP to copy files over.
1. <?php2. print ( “A simple initial script);
3.<title>HTML With PHP Embedded</title> </head>
5.<font size=5 color=”blue”>Welcome To My Page</font>
7. print ("<br> Using PHP is not hard<br>");
9.and you can learn to use it quickly!
Note! . . . You could also output the above line using the following PHP line:
print (‘<font color="blue">“’);
We will examine the meaning between “ and ‘ later.
// This is a comment
print ("A simple initial script"); //Output a line
Note: Comment lines are ignored when the script runs. They do not slow down the run-time
PHP allows a couple of additional ways to create comments.
phpinfo(); # This is a built-in function
A script that gets information about the
PHP version being used.
<? phpinfo(); ?>
The php version
This build does NOT have mysql!
Location of PHP main config file
Other interesting items:
Apache version & configuration, php config variables, environment.
Allow ASP style tages
Manual strongly recommends setting to off
Flag values indicates which errors to display/log
Global is ’85’ Local is 2047.
Show errors in browser
Include HTML errors
Note: Has option at bottom to save configuration. Also must restart apache for changes to take effect.
$x = $y + 1;
print " x=$x $y=$y"
error_reporting = E_COMPILE_ERROR|E_PARSE|E_ERROR|E_CORE_ERROR
1 + 4 + 16 + + 64 = 85.
Enable scripts to upload to server
Defines where to look for includes. (This can be set dynamically in scripts.)
Send errors to logfile
Require input variables to use super-global array
Quick function search
$days = 3;
$newdays = 100;
$days = $newdays;
print ("Bryant is $age years old.");
print (‘but george is $age years old.’);
Double quotes output any variable’s value
Single quotes output any variable’s name.
Bryant is 6 years old but george is $age years old.
$apples = 12;
$oranges = 14;
$total_fruit = $apples + $oranges;
print ("The total number of fruit is $total_fruit");
These PHP statements would output
The total number of fruit is 26.
2. <head> <title>Variable Example </title> </head>
5. $columns = 20;
6. $rows = 12;
7. $total_seats = $rows * $columns;
9. $ticket_cost = 3.75;
10. $total_revenue = $total_seats * $ticket_cost;
12. $building_cost = 300;
13. $profit = $total_revenue - $building_cost;
15. print ("Total Seats are $total_seats <br>");
16. print ("Total Revenue is $total_revenue <br>");
17. print ("Total Profit is $profit");
18. ?> </body> </html>
Use a variable without a value assigned will have no value (called a null value).
PHP will NOT generate an error and will complete the expression.
$y = 3;
$y=$y + $x + 1; // $x has a null value
print ("x=$x y=$y");
$sum = 1 + $x;
No error will occur instead will output:
$firstname = "John";
$lastname = "Smith";
$fullname = $firstname . $lastname;
$comments = "Good Job";
$len = strlen($comments);
Note: Function output would be one reason to use the concatenate operator
$First = 'George';
print "$First has " . strlen($First ) . ' characters';
George has 6 characters
Lname=“Joe JacksonJoe Jackson”
$inquote = "Now Is The Time";
$lower = strtolower($inquote);
$upper = strtoupper($inquote);
print ("upper=$upper lower=$lower");
“upper=NOW IS THE TIME lower=now is the time”.
$part = substr( $name, 0, 5);
Assign results into here
Extract from here.
Position to start extracting from.
Number of characters to extract (If omitted will continue to end of string.)
3 $url = '<a href=http://mysite.com> mysite.com </a>';
4 $store = '<font color=red> toy store </font>';
5 $special = 'boxed block sets';
8 <head> <title> this is a title </title> </head>
9 <body> <font color=blue size=4>
10 Welcome to the <?= $store ?>
11 our special today is <i> <?= $special ?> </i>
12 <br> Visit our home page at <?= $url ?>
13 </font> </body> </html>
Can separate PHP from HTML and use <?= delimiter. It automatically outputs any PHP variables found.
Enclose in square
bracket and then quotes
Name of HTML form CGI
variable (note do not use $)
Special PHP Global variable. Technically it is an associative array (covered in chptr 5.)
PHP variable name that you want to receive the HTML form input.
Use & sign
2 $cost = 5; $rows = 10;
3 $cols = 8; $total = $rows * $cols;
4 $rev = $cost * $total;
7 <head> <title> Theater Calculation </title> </head>
8 <body> <font color=blue size=4>
9 We have <?= $total ?> seats in the house
11 At a costs of $<?=$cost ?> our total possible revenue is
12 <?= $rev ?>.
13 </font> </body> </html>
$cost = 5;
$rows = $_GET[rows];
$cols = $_GET[cols];
$total = $rows * $cols;
$rev = $cost * $total;
<head> <title> Theater Calculation </title> </head>
<body> <font color=blue size=4>
We have <?= $total ?> seats in the house
At a costs of $<?=$cost ?> our total possible revenue is
<?= $rev ?>.
</font> </body> </html>
<? phpinfo() ?>