Professional Documents
Culture Documents
SSP - 4 1 PDF
SSP - 4 1 PDF
1
Another Simple Example of PHP Script
To process the form, we need to create the script say processorder.php. Open
your text editor and create this file. Type in the following code:
<html>
<head>
<title>Bob's Auto Parts - Order Results</title>
</head>
<body>
<h1>Bob's Auto Parts</h1>
<h2>Order Results</h2>
</body>
</html>
Notice, how everything we’ve typed so far is just simple HTML. It’s now
time to add some simple PHP code to our script.
2
Embedding PHP in HTML
Under the <h2> heading in your file, add the following lines:
<?php
echo '<p>Order processed.</p>';
?>
Save the file and load it in your browser (Type the following address in
address of your browser- http://localhost/processorder.php or
http://localhost:port/processorder.php). You should see something similar
to the output shown in Figure 1.2.
3
Try viewing the source from your browser. You should see this code:
<html>
<head>
<title>Bob's Auto Parts - Order Results</title>
</head>
<body>
<h1>Bob's Auto Parts</h1>
<h2>Order Results</h2>
<p>Order processed.</p></body>
</html>
Again none of the raw PHP is visible. This is because the PHP interpreter
has run through the script and replaced it.
This means that from PHP we can produce clean HTML accessible with any
browser—in other words, the user’s browser does not need to understand
PHP.
4
The code that we now have in this file consists of four things:
HTML
PHP tags
PHP statements
Whitespace
5
PHP Tags
6
PHP Tag Styles
There are actually four different styles of PHP tags we can use.
1. XML style
<?php echo '<p>Order processed.</p>'; ?>
It is the preferred tag style to use with PHP. The server administrator cannot
turn it off, it will be available on all servers. This style of tag can be used with
XML (Extensible Markup Language) documents.
7
2. Short style
<? echo '<p>Order processed.</p>';?>
This style of tag is the simplest and follows the style of an SGML (Standard
Generalized Markup Language) processing instruction. To use this type of tag you
either need to enable short_tags in your config file, or compile PHP with short tags.
3. SCRIPT style
<script language='php'> echo '<p>Order processed.</p>'; </script>
This style of tag is the longest and will be familiar if you’ve used JavaScript or
VBScript.
4. ASP style
<% echo '<p>Order processed.</p>';%>
This style of tag is the same as used inActive Server Pages (ASP). It can be used if you
have enabled the asp_tags configuration setting.
8
PHP Statements
9
Whitespace
Spacing characters such as new lines, spaces, and tabs are known as whitespace.
Browsers ignore whitespace in HTML. So does the PHP engine?
Consider these two HTML fragments:
<h1>Welcome to Bob's Auto Parts!</h1><p>What would you like to order
today?</p>
And
<h1>Welcome to Bob's Auto Parts!</h1>
<p>What would you like to order today?</p>
These two parts of HTML code produce identical output because they appear the same to
the browser. However, you are encouraged to use whitespace in your HTML as an aid to
humans—to enhance the readability of your HTML code.
10
Whitespace
The same is true for PHP. There is no need to have any whitespace
between PHP statements, but it makes the code easier to read if we put
each statement on a separate line. For example,
and
echo 'hello ';echo 'world';
12
Example of comment in PHP
13
Adding Dynamic Content
The main reason for using a server-side scripting language is to be able to provide
dynamic content to a site’s users. This is an important application because content that
changes according to a user’s needs or over time will keep visitors coming back to a
site. PHP allows us to do this easily.
Let’s start with a simple example.
Replace the PHP in processorder.php with the following code:
<?php
echo '<p>Order processed at ';
echo date('H:i, jS F');
echo '</p>';
?>
In this code, we are using PHP’s built-in date() function to tell the customer the date
and time when his order was processed. This will be different each time the script is
run.
14
Save the file and load it in your browser (Type the following address in
address of your browser- http://localhost/processorder.php or
http://localhost:port/processorder.php). You should see something similar
to the output shown in Figure 1.2.
15
Calling Functions
Look at the call to date().This is one of the calling function of PHP. PHP has
an extensive library of functions you can use when developing Web
applications.
Most of these functions need to have some data passed to them and return
some data.
Look at the function call:
date('H:i, jS F')
Notice that we are passing a string (text data) to the function inside a pair of
parentheses. This is called the function’s argument or parameter. These
arguments are the input used by the function to output some specific results.
You can recognize variable names in PHP because they all start with a dollar
sign ($). (Forgetting the dollar sign is a common programming error.)
The following statement creates a variable called $testvariable and assigns it a literal
value of 3:
$testvariable = 3;
In PHP a single variable may contain any type of data, be it a number, a string of
text, or some other kind of value, and may change types over its lifetime.
So the following statement, if you were to type it after the statement above, assigns a
new value to the existing $testvariable.
In the process, the variable changes type: where it used to contain a number, it now
contains a string of text:
$testvariable = 'Three';
17
Variables
The equals sign we used in the last statements is called the assignment operator, as
it’s used to assign values to variables. Other operators may be used to perform
various mathematical operations on values:
From the above examples, you can probably tell that + is the addition operator, - is
the subtraction operator, * is the multiplication operator, and / is the division
operator. These are all called arithmetic operators, because they perform arithmetic
on numbers.
Each of the lines above ends with a comment. Comments are away to describe what
your code is doing.
18
String concatenation operator
A period (.) between two text string sticks strings of text together and known as
string concatenation operator
Example:
20
Arithmetic Operators
We have already discussed some arithmetic operators/ Let us revise and
discuss some other arithmetic operators. The arithmetic operators are
shown in Table below
Operator Name Example
+ Addition $a + $b
- Subtraction $a - $b
* Multiplication $a * $b
/ Division $a / $b
% Modulus $a % $b
Consider this code fragment:
$a = 27;
$b = 10;
$result = $a%$b;
The value stored in the $resultvariable is the remainder when we divide 27
by 10; that is, 7.
Arithmetic Operators
The result of the modulo operator % has the same sign as the dividend — that is, the result
of $a % $b will have the same sign as $a. For example:
<?php
echo (5 % 3)."\n"; // prints 2
echo (5 % -3)."\n"; // prints 2
echo (-5 % 3)."\n"; // prints -2
echo (-5 % -3)."\n"; // prints -2
?>
$a = 3;
$a += 5; // sets $a to 8, as if we had said: $a = $a + 5;
?>
Comparison Operators
Example Name Result
$a == $b Equal TRUE if $a is equal to $b
$a === $b Identical TRUE if $a is equal to $b, and they are of the same
type.
$a != $b Not equal TRUE if $a is not equal to $b
$a <> $b Not equal TRUE if $a is not equal to $b
$a !== $b Not identical TRUE if $a is not equal to $b, or they are not of the
same type.
$a < $b Less than TRUE if $a is strictly less than $b.
$a > $b Greater than TRUE if $a is strictly greater than $b.
$a <= $b Less than or TRUE if $a is less than or equal to $b.
equal to
$a >= $b Greater than or TRUE if $a is greater than or equal to $b.
equal to
$a <=> $b Spaceship An integer less than, equal to, or greater than zero
when $a is respectively less than, equal to, or
greater than $b.
Comparison Operators
// Arrays
// Floats echo [] <=> []; // 0
echo 1.5 <=> 1.5; // 0 echo [1, 2, 3] <=> [1, 2, 3]; // 0
echo 1.5 <=> 2.5; // -1 echo [1, 2, 3] <=> []; // 1
echo 2.5 <=> 1.5; // 1 echo [1, 2, 3] <=> [1, 2, 1]; // 1
echo [1, 2, 3] <=> [1, 2, 4]; // -1
?>
Questions
PHP supports one error control operator: the At sign (@). When prepended to an expression
in PHP, any error messages that might be generated by that expression will be ignored.
<?PHP
(@include("file.php"))
OR die("Could not find file.php!");
?>
This cause, that error reporting level is set to zero also for the included file. So if there are some
errors in the included file, they will be not displayed.
Another example is
<?php
$x = @$a["name"];
?>
There are only 2 possible problems here: a missing variable or a missing index. If you're sure
you're fine with both cases, you're good to go.
Example 1:
<?php
echo 2/0; //Gives warning : Division by zero
echo @(2/0); echo 2/0; //@ suppresses the warning
}
?>
Output
In the above example, we do not get the warning "Division by zero" when we add Error
Control operator before the expression 2/0.
Example 2:
<?php
method(); //Gives Fatal Error : Call to undefined function methodCall()
echo @(2/0); echo 2/0; //@ suppresses the Fatal error
}
?>
Output
Fatal error: Call to undefined function methodCall() in
I:\xampp\htdocs\xampp\ComparisonOpearatorsExample.php on line 5
In the above example we are calling a function which has not been defined, that a
fatal error is thrown.
But when the function call is prefixed by @ operator, the fatal error is not
displayed.
Incrementing/Decrementing Operators
….
<?php echo "<h3>Postdecrement</h3>";
echo "<h3>Postincrement</h3>"; $a = 5;
$a = 5; echo "Should be 5: " . $a-- . "<br />\n";
echo "Should be 5: " . $a++ . "<br />\n"; echo "Should be 4: " . $a . "<br />\n";
echo "Should be 6: " . $a . "<br />\n";
echo "<h3>Predecrement</h3>";
echo "<h3>Preincrement</h3>"; $a = 5;
$a = 5; echo "Should be 4: " . --$a . "<br />\n";
echo "Should be 6: " . ++$a . "<br />\n"; echo "Should be 4: " . $a . "<br />\n";
echo "Should be 6: " . $a . "<br />\n"; ?>
…
Assignment
32