FREE php course Preparation #1: Build a testing Web Server
1. Use Internet Explorer to visit http://www.apachefriends.org/en/xampp-windows.html and download the
latest installer of XAMPP for Windows. As of the time this lecture is written, the latest version is 1.8.0.
2. Launch the installer file (e.g. xampp-win32-1.8.0-VC9-installer.exe)
read more about how to install xampp here
Learning Activity #3: Basic PHP Start and End tags
1.
In the “X:\xampp\htdocs\myphp” directory, use Notepad to create a new file named lab1_3.php with the
following contents:
<html>
<?php echo “Display the word ‘Welcome’ using ‘<?php … >’!”; ?><br>
<? echo “Display the word ‘Welcome’ using ‘<? … >’!”; ?><br>
<script language=”php”> echo “Display the word ‘Welcome’ using ‘<script>’!”;
</script>
</html>
2. Use a Web browser to visit http://localhost/myphp/lab1_3.php, you should see the following window.
Learning Activity #4: Commenting your PHP code FREE php course
1.
In the “X:\xampp\htdocs\myphp” directory, use Notepad to create a new file named lab1_4.php with the
following contents:
<html>
<body>
<!– This line is an HTML comment –>
<?php
//This line is a single line PHP comment.
/*This is a multiple-line comment. Line 1
Line 2
Line 3 */
# This line is a Linux/Unix shell comment
echo “Do you see any of the comment?”;
?>
</body>
</html>
2. Use a Web browser to visit http://localhost/myphp/lab1_3.php, you should see the following window.
Learning Activity #5: Using HTML tag to format PHP output FREE php course
1.
In the “X:\xampp\htdocs\myphp” directory, use Notepad to create a new file named lab1_5.php with the
following contents:
<html>
<body bgcolor=”yellow”>
<b><?php echo “PHP Programming!” ?></b><br>
<u><?php echo “PHP Programming!” ?></u><br>
<i><?php echo “PHP Programming!” ?></i><br>
<s><?php echo “PHP Programming!” ?></s><br>
<h1><?php echo “PHP Programming!” ?></h1><br>
<pre><?php echo “PHP Programming!” ?></pre><br>
<center><?php echo “PHP Programming!” ?></center><br>
</body></html>
Use a Web browser to visit http://localhost/myphp/lab1_3.php, you should see the following window.
Lecture #2: PHP Variables, Data Types, and Expressions
Definition of data type
Human languages, such as English, use combinations of character to represent data. In English,
characters include alpha characters (lowercase and uppercase), numerals (0, 1, 2, .., 9), and symbols
($,#,*,&..). For example:
• Jennifer represents a girl’s first name, and it is a combination of alphabets.
• 714-852-9634 is a phone number, and it is a combination of numerals and symbol (-).
• D009865471 is a student ID, and it is a combination of alphabet and numerals
Different data can use different combinations of alpha characters, numerals, and symbols; therefore,
a data type is set of data with values having predefined characteristics and format. A data type is a
specification that assures the use of a specific set of values.
Usually a limited number of data types are built into a programming language. The language usually
specifies the range of values for a given data type, how the computer processes the values, and how
they are stored. In other words, the data type defines how computers should process a given variable
( of that data type).
What is a variable?
In programming languages, a variable is a language object that may hold different values for a period
of time. The value of a variable is usually restricted to a certain data type, such as an integer or
floating-point number.
A variable can be thought of as a programming construct used to store both numeric and non-
numeric data.In some cases, the contents of a variable can be changed during program execution. A
variable needs a name to be used to represent a data item whose value can be changed while the
program is running.
Variables in PHP are represented by the dollar sign prefix followed by the name of the variable. The
variable name is case-sensitive. For example:
$name;
$num;
Usually, the value of a given variable is provided by the user. For example, you enter your age to the
text box and then click Go. Your age will be assigned to a variable as value and is later retrieved and
inserted to the next sentence. You can keep on entering different numbers. See the example PHP
code below.
<?php
$age = $_POST[“age”];
?>
…….
<form action=”test.php” method=”post”>
<input type=”text” name=”age”>
…….
The above PHP example declares a variable with a valid name (e.g., $age) without assigning initial
value (meaning it has a null value). The “$age = $_POST[“age”];” line will accept whatever
value the user assigns to the $age through a form. When the user enters a value (e.g., 19), the user
indeed assigns a value to the variable dynamically.
PHP Expressions
PHP expressions are the building blocks of a PHP code. In PHP, almost any code you write is an
expression. The PHP manual defines a PHP expression as “anything that has a value”.
The most basic forms of expressions are constants and variables. When you type $a = 5, you assign
“5” to the variable, $a. In other words $a = 5 is an expression that the variable $a has the value of 5.
After this assignment, if you wrote $b = $a, you technically express your wish of $b = 5.
Some expressions can be considered as statements. In this case, a statement has the form of
expression;
namely, an expression followed by a semicolon. The following example is a expression and a valid
PHP statement.
$b = $a = 5;
Given an expression, $a = 5. There are two objects involved, the value of the integer 5 and the
variable $a which is being assigned with 5. It simply means that $a = 5. Regardless of what it does, it
is an expression that $a is given the value 5. Thus, writing something like “$b = $a = 5;” or “$b
= ($a = 5);” is equivalent to writing two statements: “$a = 5;” and “$b = 5;” (a semicolon
marks the end of a statement). Since assignments are parsed in a right to left order, you need to write
$b = $a = 5; (or “$a = $b = 5;”).
Concept of using escaped sequence
PHP reserves many characters to help processing values held by variables, such as single quote (‘),
double quote (‘), dollar sign ($), and others. In other words, such reserved characters are not treated
as string literals as in plain English.
Sometimes it is necessary to display special PHP reserved characters as text. In this case, you will
then need to escape the character with a backslash (\). PHP uses the backslash (\) character to
restore and/or redefine characters that otherwise have a special PHP meaning, such as the newline
backslash itself (\n), or the quote character. The restoration/redefinition is known as a “escape
sequence “.
In many languages, escape sequences are used to format values of variables.
For example, review the following PHP code:
<?php
echo “A newline starts here \n”;
echo “A carriage return is \r”;
echo “A tab is \t.”;
echo “A dollar sign is \$”;
echo “A double-quote is \””;
?>
The output should be:
A newline starts here
A carriage return is
A tab is
.A dollar sign is $A double-quote is ”
Important Note!
There is a critical problem with using an escaped sequence with web browser–the web browser will
simply ignores blank spaces. And that explains why you did not get the above results when you test
them with web browser. You can use the “View Source” function of the browser to view the correct
output.
Basic PHP data types
PHP supports a number of different data types. The common ones are integers, floating point
numbers, strings, and arrays. In many languages, it’s essential to specify the variable type before
using it.
For example, a variable may need to be specified as type integer or type array. A list of
some of the PHP data types and their definitions are:
• integer: An integer is a plain whole number like 35, -80, 1527 or 1. They cannot have fractional
part.
• floating-point: A floating-point number is typically a fractional number such as 18.5 or
3.141592653589. Floating point numbers may be specified using either decimal or scientific
notation.
• string: A string is a sequence of characters, like “hello world” or “abracadabra”. String values
may be enclosed in either double quotes (“ and ”) or single quotes(‘ and ’). A blank space inside
the quotes is also considered a character.
• boolean: A Boolean variable simply specifies a true or false value.
• array: An array is a collection of items that have something in common. A detailed discussion is
given in a later lecture.
Variables in PHP are represented by a dollar sign ($) followed by the name of the variable. For
example, $popeye, $one and $INCOME are all valid PHP variable names. The variable name is
case-sensitive.
To assign a value to a variable, you use the assignment operator, the “=” sign. For example, review
the following PHP code:
<?php
$myvar = “Jennifer”;
$Myvar = “Lopez”;
echo “$Myvar, $myvar”;
?>
The output will be:
Lopez, Jennifer
Both $myvar and $Myvar represent two different variables. The “=” sign is the assignment sign.
$myvar is assigned a value “Jennifer”, while $Myvar is assigned another value “Lopez”.
The echo command displays both values to the screen. The value being assigned need not always be fixed; it
could also be another variable, an expression, or even an expression involving other variables.
For example:
<?php
$dog_age=10;
$human_age = $dog_age * 7;
echo “Your dog is $human_age years old in human age.”;
?>
A variable $dog_age is declared and assigned a value 10. The value of $dog_age variable is then
multiplied by 7, giving a value of 70, as the value of the $human_age variable.
The echo command display the value of $human_age, and the output will be:
Your dog is 70 years old in human age.
Interestingly enough, you can also perform more than one assignment at a time
. Consider the following example, which simultaneously assigns three variables to the same value:
<?php
$price1 = $price2 = $price = 15;
?>
Boolean
Boolean variables are commonly used for decision making. For example:
<?php
if ($add_line == TRUE) {
echo “<hr>”;
}
?>
Note: The above example uses if..then statement for decision making. A detailed discussion about
how to use decisive logic is given in later lecture.
Integers
In PHP, integers can be based in decimal (10-based), hexadecimal (16-based) ,or octal (8-based)
notation, optionally preceded by a sign (- or +).
Important concept:
• Decimal numbers have numerals 0, 1, 2, 3, 4, 5, 6, 7, 8, and 9.
• Octal numbers have numerals 0, 1, 2, 3, 4, 5, 6, and 7.
• Hexadecimal numbers have numerals 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, A, B, C, D, E, and F.
To define the base of a number, use the following rules:
• if the default base is decimal, there is no need to precede the number with anything.
• to use the octal notation, you must precede the number with a 0 (zero).
• to use hexadecimal notation precede the number with 0x (zeroX).
To specify a positive or negative value, use the following rules:
• numbers are positive by default.
• to specify the negative value, precede the number with a negative “-” sign.
For example,
<?php
$myNo1 = 5678; // decimal number
$myNo2 = -5678; // a negative number
$myNo3 = 0123; // octal number (equivalent to 83 decimal)
$myNo4 = 0x1A; // hexadecimal number (equivalent to 26 decimal)
echo “$myNo1, $myNo2, $myNo3, $myNo4”;
?>
FREE php course ,Floating point numbers
Floating point numbers (also known as “floats”, “doubles” or “real numbers”) can be specified using
any of the following syntax:
• Math format. Simply show the integer and the decimal fractions, such as 3.14125.
• Scientific format. In scientific format, the natural logarithm (e) is used to denote the exponent.
For example, 6.25X1015 is expressed as 6.25+e15 (or simply 6.25e15), while 2.74X10-23 is 2.74-
e23. The natural logarithm letter distinction (e) is not case sensitive. You can use “e” or “E” to
express scientific formatted numbers.
Consider the following example:
<?php
$a = 3.125;
$b = 1.2e3;
$c = 5.27E-10;
echo “$a <br>”;
echo “$b <br>”;
echo “$c <br>”;
?>
The output should look like this:
3.125
1200
5.27E-10
Strings
A string is series of characters. A string literal can be specified in two different ways.
• Single quoted. The easiest way to specify a simple string is to enclose it in single quotes (the
character ‘). But, if you use single quotes (‘) for your string, all characters are printed as-is.
• Double quoted. In PHP, escape sequences only work with double quotes (“).
Consider the following example with double quotes,
<?php
echo “Line 1\nLine 2”;
?>
The output should be:
Line 1
Line 2
However, when using single quotes,
<?php
echo ‘Line 1\nLine 2’;
?>
the output should change to (however, the \n is ignored by web browser):
Line 1\nLine 2
The most important difference between the two is that a string enclosed in double quotes is parsed
by PHP.
This means that any variable in it will be expanded.
<?php
$var = ‘apple<br>’;
echo “$var”; // Results in the value of $var being printed
echo ‘$var’; // Results in the word ‘$var’
?>
The output is then:
apple
$var
PHP Variable
There are a few rules that you need to follow when choosing a name for your PHP variables.
• PHP variables must start with a letter – an alpha charater.
Naming Conventions
• PHP variables may only be comprised of alpha-numeric characters and underscores. a-z, A-Z, 0-
9, or _ .
• Variables with more than one word should separate each word with an underscore character
( _ ). For example, $my_variable .
Do not use keywords and reserved words to name variable. They have special meaning in PHP.
Some keywords and reserved words represent functions, constants, or other PHP features.
According to PHP manual (http://us3.php.net/manual/en), $this is a special variable name that
cannot be assigned.
FREE php course , Predefined variables
PHP provides a large number of predefined variables. They are a preferred method for retrieving
external variables. PHP itself creates some of the variables, while many of the other variables change
depending on which operating system and Web server PHP is running. Rather than attempt to
compile a complete listing of available predefined variables, you can run the following script to
display all of the available variables.
<?php
// Show all information, defaults to INFO_ALL
phpinfo();
?>
The following example will display the filename of the currently executing script, the type of web
browser or user agent that is being used to access the current script, the server’s IP address, the
server’s host name, and so on.
<?php
echo $_SERVER[‘PHP_SELF’] . “<br>”;
echo $_SERVER[‘HTTP_USER_AGENT’] . “<br>”;
echo $_SERVER[‘REMOTE_ADDR’] . “<br>”;
echo $_SERVER[‘REQUEST_METHOD’] . “<br>”;
echo $_SERVER[‘SERVER_NAME’] . “<br>”;
echo $_SERVER[‘SERVER_SOFTWARE’] . “<br>”;
echo $_SERVER[‘SERVER_PROTOCOL’];
?>
You can use the following common predefined variable to retrieve information.
To view a comprehensive list of Web server, environment, and PHP variables offered on your
particular system setup, simply visits the demo site
at:http://us3.php.net/manual/en/reserved.variables.server.php.
Review Questions
1. Which escape sequence is equivalent to the [Tab] key?
A. \tab
B. /tab
C. \t
D. /t
2. Given the following code, what is the output?
<?php
$x = $y = 2;
echo ($y);
?>
A. 11
B. 9
C. 8
D. 2
3. Which is a string?
$a = “Hello, world!”;
$b = “1 2/5”;
$c = “02.23.72”;
A. $a
B. $b
C. $c
D. All of the above
4. Which is a floating-point variable?
$a = 12;
$b = 972;
$c = 15.48;
$d = -43;
A. $a
B. $b
C. $c
D. $d
5. In the following code, $larger is a __ variable.
<?php $larger = TRUE; ?>
A. Comparison
B. Boolean
C. Floating-point
D. Logical
6. The output of the following code is __.
<?php echo 0x1A; ?>
A. 25
B. 26
C. 81
D. 83
7. The output of the following code is __.
<?php
$x = 5;
$z = $y = $x;
echo $z;
?>
A. 5
B. $y
C. $y = $x
D. an error message
8. The output of the following code is __.
<?php
$y = ($x = 7);
echo $y;
?>
A. ($x = 7)
B. 7
C. $x = 7
D. $x
9. Which is a floating-point variable?
<?php
$a = 3.125;
$b = 1.2e3;
$c = 5.27E-10;
?>
A. $a
B. $b
C. $c
D. All of the above
10. Which is a possible output of the following code?
<?php echo $_SERVER[‘SERVER_NAME’]; ?>
A. CISDEpt.com
B. 207.125.11.110
C. SNMP
D. /usr/cis246/test.php
FIND ALL COURSE LECTURES ON THIS LINK