Learn PHP in A Day
Learn PHP in A Day
Copyright 2015
All rights reserved. No portion of this book may be reproduced
mechanically, electronically, or by any other means,
including photocopying without the permission of the publisher
Disclaimer
The information provided in this book is designed to provide helpful information on the subjects
discussed. The authors books are only meant to provide the reader with the basics knowledge of a
certain topic, without any warranties regarding whether the student will, or will not, be able to
incorporate and apply all the information provided. Although the writer will make his best effort
share his insights [bun cunoatere, nelegere, perspicacitate], learning is a difficult task and
each person needs a different timeframe to fully incorporate a new topic. This book, nor any of the
authors books constitute a promise that the reader will learn a certain topic within a certain
timeframe.
Contents
Preface
Chapter One Introduction, Setup and Hello World
Introduction to PHP
Setting up Our Work Environment
Our Very First PHP file
Chapter Two Variables
Introduction to Variables
Ba s i c s
Rules
Variable Types and Typecasting
Boolean
Numbe
r String
Array
Objects
NULL
Conclusion
Chapter Three Logical, Math and other Expressions and Operations
Introduction to Expressions and Operators
Expressions
Operators
Conclusion
Chapter Four Control Structures
Introduction
If statements
If Statements
If Else Statements
If-elseif-else statement
Sw itch
Alternate syntax to control structures
While
Do-while
For
Foreach
Break
Continue
Return
Include
Require
Require_onc
e
Include_once
Conclusion
Chapter Five Functions
Introduction
User Defined Functions
Function Arguments
Return values
Variable Scope
Pre-defined functions
Echo, Print, exit, die
String functions
Conclusion
Chapter Six Databases
Introduction
What is an API?
Connectors
Drivers
Extensions
PHP MySQL APIs
mysqli Extension
PDO Extension
PHPMyAdmin and getting familiar with MySQL
Ba s i c s
MySQLi
Dual interface
Connections
Executing
statements Prepared
statements
Conclusion
Chapter Seven Form Data
Introduction
Methods
for sending Form Data
Referencing information from forms
Security
Conclusion
Chapter
Eight Sessions and Cookies
Introduction
Sessions
Passing the Session ID
Custom Session Handlers
Cookies
Creating and retrieving cookies with PHP
Modifying a cookie using PHP
Deleting a cookie using PHP
Check if Cookies are enabled using PHP
Conclusion
Chapter Nine File Handling
Introduction
File Handling
Reading Files
Opening files
PHP Reading files
PHP Closing files
PHP Create File
PHP Write to File
PHP Overwriting
Conclusion
Chapter
Ten Object Oriented Programming
Introduction
Ba s i c s
Cl a s s
new
extends
Properties
Constants
Autoloading Classes
Constructors and destructors
Object Inheritance
Scope Resolution Operator (::)
Conclusion
Answers to Exercises
Chapter2
Chapter3
Chapter4
Chapter5
Chapter6
Chapter7
Chapter8
Chapter9
Chapter 10
Preface
1.
Welcome to this Introductory Course to PHP. Throughout this course, you will learn the basics
behind using PHP in your own projects. You will be introduced to some of the most commonly
used functions and methods as well as PHP best practices. This course will mostly teach by
example, emphasizing a show, dont tell approach. We will guide you through different realworld examples of PHP applications and explain every step of the process so that you understand
not only how, but also why we are writing our application the way we are.
The course is structured around different chapters, where each chapter is focusing on a
particular aspect of programming with PHP. At the end of each chapter, there will be a chapter
summary and a chapter exercise that will test all the skills you have learned in the chapter thus far,
as well as build on top of previous knowledge.
You will be presented with code at every step of the way. Code will be easily recognized as it
will be separated from the rest of the text, and will be formatted differently. Here is an example of
a code block:
With all of that out of the way, lets get started with the course!
Navigate to the installation directory of your LAMP bundle. In my case it looks something like:
C:\xampp\htdocs . htdocs is the directory that contains all of your websites. Generally, for the sake
of clarity, I like creating a websites directory inside of htdocs and working from there. In either
case, create a new folder for your website and give it a name. Try to refrain from using spaces (use
_ instead). Inside that folder, create an index.php file. Open the file in your text editor and type the
following lines of code:
All PHP code is wrapped within the PHP tags ( <?php ?> ). Notice that we say all PHP code, not
all PHP files. That is, you can have things such as HTML inside a .php file. More on that in
another chapter, though.
Some more rules to keep in mind in terms of syntax:
-
Comments in PHP can be single-line or multi-line. Single line comments start with two
backslashes ( // ). Anything placed after these slashes is considered a comment and will not be
executed by the compiler. Multi-line comments start with a forward slash and a star and end with
a star and forward slash ( /* Comment goes here*/ ). Again, the compiler will ignore anything placed
inside of these symbols.
Here is an example of comments in code:
Multi-line comments are usually used for longer descriptions of blocks of code, while single-line
comments are used (as in the example above), when you want to describe what a certain line of
code does. Multi-line comments can be also useful for debugging. For example, if you have a
portion of code that is not working the way you want it to, you can comment it off by using
multi-line comments and tests bits of it to see what exactly goes wrong.
There is no rule in PHP that tells you whether to use comments or not to, but it is
considered good practice to use comments. If you are going to share your code with other people,
you should definitely (and by definitely, we mean always!) use comments. What seems obvious
to you will not be so obvious to another coder. Furthermore, even if you are not going to be
sharing your code, but are instead just writing some file that you know will be for yourself, you
should still make use of comments (even if you dont comment as extensively as you would if you
were sharing). Very often you will find yourself needing to revisit old code and when that
happens you would want to quickly be able to remember what you were doing and why you were
doing it. Although they dont add functionality to your code, comments are just as important as
the code you write, so dont ignore them!
Okay, after that digression, lets return to our first PHP file. Open your web browser
of choice and enter the following URL: localhost/websites/your_folder_name_here/index.php or
remove the websites if your new website is in the htdocs folder.
You should see the words Hello World! being displayed at the top of the web page.
Hurray! Our very first PHP file is done! But that wasnt very interesting, was it? Lets
move on to chapter two where we learn about the different types of variables inside PHP and start
having some more fun!
Rules
There are rules to keep in mind when creating variables
inside of PHP. Well list all of them and after that we will take a look
at code that shows these. Here are the basic rules:
Variable names start with
a letter or underscore;
Variable names cannot
start with a number;
A variable name can have any number of
letters, underscores of numbers; Valid
letters are all ASCII hexadecimal letters
from 00 to FF;
Boolean
Booleans are the simplest type of variables that exist in PHP. Booleans are used for
logical statements and to express a truth value. Booleans are binary, meaning that they take one
of two values: TRUE or FALSE . Having said that, lets look at an example of a Boolean variable:
Like many other languages, PHP is flexible in its use and interpretation of Boolean
variables. In other words, we can create Booleans without explicitly defining them as either
being TRUE or FALSE. IN fact, here is the list of rules that determines which values are
considered to be FALSE by PHP:
The Boolean FALSE ;
The integer 0 ;
The float 0.0 ;
The empty string and the string
0 ; An array with zero elements;
The special type NULL ;
Even though we still havent covered if statements, you can pretty much figure out what
will happen here. The implicit Boolean variable is defined inside the parenthesis (). Once the
logical operation executes it will return either a TRUE or a FALSE.
A couple of important things to note about the above example:
PHP considers the integer 1 to be the same as TRUE. That means, if we have defined the
variable var to be TRUE, and we check it against the integer one, the logical statement will
return TRUE:
Number
Numbers in PHP, and most programming languages, come in different flavors. The two
subtypes of numbers in PHP are Integers and Floating point numbers. Lets take a look at each
one.
Integers
By definition, integers are all numbers in the set:
In other words, integers are all whole, real numbers (both positive, negative and zero). PHP
allows us to define integers in a number of ways. You can define integers in base 10, base 8
(octal), base 16 (hexadecimal) or binary (base 2).In order to use octal notation for an integer, you
need to precede the number with a 0 . To use hexadecimal notation for an integer, you precede the
number with an 0x . Lastly, if you want to use binary notation, you precede the number with 0b .
Here is an example:
Take note of the last function we use (dont worry if you dont know what functions are).
Helpful Tip!
You can check the type and contents of a variable by using var_dump(). The function takes one input
as an argument (again, dont worry if you dont fully understand. This means that you should
only put one thing inside the parenthesis). The function will print the type of the variable and the
contents to the screen. In the above example, the output to the screen will look like:
Now try to
use var_dump on the other variables. Do you notice anything interesting? Do you see the value
that is being printed to the screen for each variable? What base is it in?
The largest number that can be stored on a 32-bit system as an integer is of magnitude
21474783647. Anything larger than that will automatically be converted to a type of float . For a
64-bit system the number is of magnitude 9223372036854775807. Anything larger than that will
automatically be converted to a type of float .
If you want to convert a variable to an integer, in other words cast it as an integer, you would do
the following:
This type of casting is useful in a number of situations, but the most common usage is for security.
One of the exercises at the end of the chapter will show you a particular usage of integer casting
for security purposes.
Floating Point Numbers
Integers was the set of real and whole numbers, so logically, floating point numbers (also
referred to as floats, doubles or real numbers) will include everything else. That is, the
numbers that in between integers. Thus, we get the set of all real numbers. With that being said, it
is crucial to note that all computers have a limitation as to the precision with which they display
numbers (this is known as a machine epsilon). Furthermore, there are some weird discrepancies
that may occur when dealing with floats. We wont go too much into that since it is beyond the
scope of this book, but if you find any non-intuitive results when dealing with floats, think about
the precision with which PHP handles floats.
Floats can be represented in the following ways in PHP:
String
Stings in PHP are series of characters. You use these to store pieces of text and words.
PHP handles each character as a byte. With that being said, we should note that the largest string
that PHP can support is of size 2GB. The fun bits come when defining strings.
Single quotes
The easiest way to define a string in PHP is to use single quotes.
This is valid syntax inside of PHP, but the newline break will not be incorporated into the final
output. Not the use of the echo function. What does the above code output?
Helpful Tip!
You can use the echo command to output strings to the screen. In fact, echo can be used with any of
the variable types to output their contents other than the array and object types. This is very useful
tool for debugging and you should often use it when you need to check something quickly or to
find errors in your code.
OK, now imagine you want to input a string that contains a single quotation mark. What do you
think the output is going to be?
You can probably tell even from the syntax highlighting that this is not going to work. Every time
PHP encounters a single quotation mark, it will either start or end a string variable. In order to
store a single quotation mark, we need to escape it. In PHP, this is done using the backslash. The
same thing will happen if we want to include a backslash. We have to escape it with another
backslash:
Use echo to see the results above. Keep in mind that a backslash is used for escaping.(This means
that \n will not render as a newline)
Another thing that cannot be used inside single quotation marks is variables. For examples, the
following will not expand inside of single quotation marks.
This will not display Her name is Katherine as you would expect.
In order to make the above example work, we need to concatenate the variable and the string. In
PHP we would do the following:
Test out the above code and confirm that it works.If you want to continue the string after the
variable, you just concatenate the next string to the variable. In other words you use $str.your string
here . This works as long as the variable can be cast into a string (PHP does that automatically for
you in this case).
Double quotes
This is the more complex way of defining strings in PHP, but it gives you a lot more control and
flexibility. In the previous examples, we saw that \n, \t, \r and similar will not work in single
quotes. This is not the case with double quotes. Double quotes allow you to use those special
symbols.
The most useful thing about double quotes, however, is their ability to parse variables. There are
two ways to parse variables inside of double quotation marks.
Simple variable parsing
The simple way to parse variables is by just using the dollar sign inside of your string:
You can use this same concept if you want to reference elements of an array. (You should
probably come back to this section after reading about the next variable type).
This is called complex because it allows you to create more complex expressions, not because the
method itself is complex. This method requires you to use curly braces around your variables.
You can expand this and test it out on things such as arrays, objects and functions once you learn
about those.
String casting
As with all other variables, we can cast variables to strings. To do this you use the (string)
function. PHP will automatically convert variables to strings depending on the scope of the
expression and if a string is required. For example, if you use echo on a variable, it will first
convert it to a string.Numbers will be converted to their textual representation. This means that 10
will become 10 .
Arrays and objects behave a bit differently, though. If you use echo on an array , you will get the
string Array . Similarly, if you use echo on an object, you will get the string Object .
Array
An array is a type of ordered map. What this means is that arrays will associate values to
specific keys. Arrays in PHP can be used to represent dictionaries, vectors, lists, stacks,
queues, trees and multidimensional arrays (arrays of arrays).
Arrays are created in the following way:
Note how we have used indentation for the sake of clarity. The extra whitespace is ignored by
PHP, but it makes our code easier to read and manage.
Each line of the array represents a key-value pair. Each line ends with a comma to indicate the
next key-value pair in the array. The last entry of the array does not need to have a comma at the
end of it, but most people like to include it. This convention is adopted by many people dealing
with content management systems such as WordPress, Drupal and others. (The idea of leaving the
comma is that you can take the entire line of key-value pairs and move it around in the array
without having to worry about including a comma).
The keys of an array can be either integers or strings. If you use something else for the array key,
it will automatically be cast into either of these types. (NOTE: arrays and objects cannot be used
as array keys as this will result in an error).
Alternatively, you do not need always need to supply the key. You can do the following:
This will create keys for you automatically by incrementing each key, starting from 0. We can
check the contents of the array:
You can see that the first entry of the array starts from key 0. It is important that you remember
that the first value in an array with default keys is always going to be stored under key 0.
You can reference elements of an array using the square brackets.
Play around with this syntax and try to create arrays of arrays (multidimensional arrays). In other
words, try to have an array as the value of some key. This looks like:
If you want to create new elements to an existing array, or to modify elements of an existing array
you use the square bracket syntax:
Objects
Objects will be an object of discussion in Chapter 10 and we shall leave them for now.
NULL
The NULL value is special and it represents a variable with no value. The only possible way of
that happening is if the variable is assigned the constant NULL, the variable has not been assigned
a value yet, or if the variable has been unset (remember the last example from the array section
when we unset the value of the 2nd entry).
Conclusion
This is the end of this chapter! You learned a lot about PHP and how it works. Youve taken your
first steps, steps that you will use over and over again when writing code. This chapter may have
been a little boring in some bits, but it is important you learn these basics well so you can apply
them
without having to think about it later on. Here are some exercises to get your gears going and get
you thinking about using what youve learned.
Exercise 1
You are given the following array of user data. Perform the specified manipulations to it:
1. Create a string that consists of the first and last name of the user by concatenating the
values from the array.
2. Suppose that this array will be stored into a database of other users. We want to make
sure that all values will be of the correct type.
1. Cast the user ID to a type of integer;
2. Cast the admin field to a type of Boolean;
3. Add a new field with a key of email and set it to the value of: [email protected] .
Exercise 2
Create a menu for a restaurant. To do this, you have to use a multidimensional array. Your menu
has to include three main subcategories (name your array menu and the keys inside this array will
be your main subcategories). Each subcategory will include 4 meals/foods. For each meal/food
you need to include a list of ingredients and a list of nutritional information (carbs, sugars,
protein, etc.). You have to figure out the most efficient way to do this inside of PHP. (Hint: Think
about how you want
to create and organize the information for each meal. Do you want to make the key the name of
the meal, or do you want to set the key to be automatically incremented and include the name of
the meal inside the array? Which makes the most sense to you? Which causes the least confusion?
Sketch it out on paper if need be).
Exercise 3
Imagine you are creating an online application for a restaurant and you are tasked with creating
the section for deliveries. Use the array from the previous exercise to provide yourself with the
data for the menu. Create a variable that represents one of the three main subcategories (it should
have the same exact name). Now create a variable that corresponds to one of the foods you made.
Suppose that these variables represent the choice made by a user when selecting their meal.
Create a string that would be a message displayed to the user with important information about
their order. It should look like this:
You picked: {name of meal here}.
This meal is made from: {list ingredients here}.
The nutritional information for this meal is: {list nutritional information here}.
Operators
The technical definition of an operator in PHP is something that takes one or more values (or
expressions, in programming jargon) and yields another value (so that the construction itself
becomes an expression).
Grouping of operators happens according to the number of values that the operator acts upon.
There are unary operators, such as the logical NOT operator or the increments operators ( !, ++,
-- respectively). Binary operators take two values. These are the familiar math operators that we
all know ( +, -, etc. ). There is also a single ternary operator that we will go into more detail when
discussing logical operators.
Lets go into analyzing the types of operators.
Operator Precedence
Operator precedence is used to determine how tightly an operator binds two expressions to one
another. Operator precedence is used in cases when you want to determine the result of an
expression such as 1+2*4 where parentheses are not used to force precedence.
When operators have equal precedence, their associativity decides how the operators are to be
grouped. For example, if we write 1-2-4 this will give us -5 because the minus sign ( - ) is leftassociative.
Like in regular everyday math, you can use parentheses in PHP to force the precedence of an
operator. It is generally advised to use parentheses even if you have written your expression in a
way so you dont need them. Using parenthesis is a best practice that makes code more human
readable and easier to understand.
Here is a comprehensive list of all operator precedence:
Associativity
Non-associative
Operators
clone new
Left
Right
**
Right
Right
Left
!
*/%
Left
+-.
Non-associative
Left
Left
Left
More Information
Used to create a clone
of an object or a new
instance of an object
respectively
Used when
reading/writing to
elements of an
array.
Used for exponentiation
in PHP
These are the increment
and decrement operators
and the type casting
operators and the error
control operator (more
info about these later)
Logical negation operator
Arithmetic operators for
multiplication, division
and
modulo
(remainder).
Arithmetic operators for
addition and subtraction
as well as string
concatenation.
Comparison operators
Logical AND operator
Logical OR operator
Ternary operator
Right
Left
Left
= += -= *= **= /= .= %=
&= |= ^= <<= >>=
and
or
Assignment operators
Logical operator
Logical operator
Name
Negation
$var + $var2
Addition
$var - $var2
Subtraction
$var *
$var2
Multiplication
$var / $var2
Division
$var % $var2
Modulus
$var ** $var2
Exponentiation
Result
Opposite (negative) of the
variable.
The sum of the
two variables.
The difference of the two
variables.
The product of the
The quotient of the
two variables
The remainder the
first variable divided
by the second.
The result obtained by
raising the first variable
to the power
corresponding to the
second variable.
When using the division operator, it is good to keep in mind that the result will be a float unless
the two operands are integers that are evenly divisible. If the two operands are integers that are
not evenly divisible, the returned value will be of type float.
The operands of the modulus operator are converted to integers before processing. The decimal
part of the value is stripped and the remaining integer is used.
The result of the modulus operator takes its sign from the dividend.
Lets take a look at a couple of examples.
Assignment[atribuire] Operators
The most basic assignment operator is = . You might be tempted to think of this as an equals
to, but that would be a mistake. This operator actually means that the left operand gets the value
of the expression on the right. So essentially you can think of this as gets sets to. With this
knowledge, we can do some tricky and interesting things.
When dealing with arrays, assigning a value to a named key is done using the => operator.
The precedence of this operator is the same as the precedence of all assignment operators.
In addition to the basic assignment operator, there are a number of combined operators that come
in handy when we want to use shorthand for binary arithmetic operations. Lets say we have a
variable $a equal to some value. In a later part of the code we want to set $a to be the previous
value of $a plus some other value. To do that, we can write:
A simpler way of doing the above would be to use the combined operator for addition:
These
combined operators work for all arithmetic operations as well as for string concatenation.
Here are a few examples:
Comparison Operators
Comparison operators allow us to compare two values. Lets take a look at a list of
comparison operators.
Example
$var == $var2
Name
Equal
Identical
$var != $var2
Not equal
$var <>
$var2
Not equal
Not identical
Result
Returns TRUE if the two
variables are equal. Type
juggling will be
implemented if
necessary.
Returns TRUE if the two
variables are equal and of
the same type.
Returns TRUE if the two
variables are not equal
after type juggling.
Returns TRUE if the two
variables are not equal
after type juggling.
Returns TRUE if the two
variables are not equal or
they are not of the same
type.
Less than
Greater than
If you are comparing a string with a number or the comparison is between numerical strings, the
strings are converted to numbers and the comparison is performed numerically.The type
conversion is not done when using === or !== as these operators check for type as well as value.
Another conditional operator to be aware of is the ternary[ Care este compus din trei uniti
sau din trei elemente] operator. The ternary operator is essentially a short hand for the if/else
control structure (more on that later). The ternary operator has the form (some conditional expression) ?
{do this if true} : {do this if false} . (The curly brackets are not used, they just there for clarity). Here is an
example of a ternary operator as you would use it in an application and the corresponding if/else
statement that it represents:
You can see that using the ternary operator is a lot shorter than writing out the entire if/else
statement. This concludes our overview of comparison operators. We have not extensively covered
the
comparison between variables of different types, but such information can be found in the PHP
documentation available online so we have decided not to include it.
Error Control Operators
The error control operator is something that should be used cautiously. In PHP, the error
control operator is the at sign ( @ ). When an expression is prepended with the error control
operator, any error messages associated with that expression that would normally be generated
will be ignored.
Using this operator has its downsides because if you prepend it to an expression that generates
an error that causes script termination, the script will stop without telling you why, which is
bad for debugging.
We will not go into examples of using the error control operator, as you would rarely have to use
it and should try to form your code so that you do not rely on it.
Incrementing and Decrementing Operators
The incrementing and decrementing operators are the ones you would see in C-style languages.
These operators affect numbers and strings but not arrays and objects. Decrementing NULL values
has no effect, but incrementing a NULL value results in 1.
Here is a list of the incrementing and decrementing operators.
Example
++$var
Name
Pre-increment
Result
Increments the variable by
$var++
Post-increment
--$var
Pre-decrement
$var--
Post-decrement
Logical Operators
PHP supports logical operators that allow us to evaluate logical statements. Here is a list of
logical operators
Example
$a and $b
Name
And
$a or $b
Or
$a xor $b
Xor (either)
!$a
Not
$a && $b
And
$a || $b
Or
Result
Returns TRUE if both
variables are true.
Returns TRUE if either
one of the two variables
is true.
Returns TRUE if either
one of the variables is
true, but not both.
Returns TRUE if he
variable is not
TRUE.
Returns TRUE if both
variables are true.
Returns TRUE if either
one of the two variables
is true.
There are two logical operators for AND and for OR because they have a different order of
precedence. The most common usage of logical operators is within an if/else statement or within
the first expression of a ternary operator. Lets look at some examples of logical operators and
how they are used.
String Operators
There are only two types of string operators and we have already seen both of them. The first
operator is the dot operator which concatenates two strings. The second operator is the
combined
concatenating assignment operator which appends the argument on the right side to the argument on
the left side. You will use both very frequently.
<?php
$a = "Hello ";
$b = $a . "World!"; II now $b contains "Hello World!"
$a = "Hello ";
$a .= "World!";
?>
Conclusion
This concludes our overview of the different types of operators and expressions inside of PHP.
You are getting closer and closer to writing your first real PHP applications! You need two more
basic building blocks, covered in the next two chapters and you will be on your way to coding
intermediate and advanced applications!
Exercise 1
Arithmetic-assignment operators perform an arithmetic operation on the variable at the same time
as assigning a new value. For this PHP exercise, write a script to reproduce the output below.
Manipulate only one variable using no simple arithmetic operators to produce the values given in
the statements.
Hint: In the script each statement ends with "Value is now $variable."
Value is now 8.
Add 2. Value is now 10.
Subtract 4. Value is now 6.
Multiply by 5. Value is now 30.
Divide by 3. Value is now 10.
Increment value by one. Value is now
11. Decrement value by one. Value is
now 10.
Exercise 2
For this PHP exercise, write a script using the following variable:
$around="around";
Single quotes and double quotes don't work the same way in PHP. Using single quotes (' ') and the
concatenation operator, echo the following to the browser, using the variable you created:
What goes around comes around.
Exercise 3
PHP allows several different types of variables. For this PHP exercise, you will create one
variable and assign it different values, then test its type for each value.
Write a script using one variable $whatsit to print the following to the browser. Your echo
statements may include no words except Value is. In other words, use the function that will
output the variable type to get the requested text. Use simple HTML to print each statement on its
own line and add a relevant title to your page. Include line breaks in your code to produce clean,
readable HTML.
Value is
string.
Value is
double.
Value is
boolean.
Value is
integer.
Value is
If statements
If Statements
The if statement is one of the most important structures inside any language.This control structure
allows you to have conditional execution of code fragments. PHPs if structure is similar to that
of
C. That is, each if statement at its most basic looks like if (expression) statement . The expression
is evaluated to its Boolean value. If the expression is TRUE, PHP will execute the
statement. If it is FALSE, it will not execute it. A simple example is:
Very often you would need to execute more than one statement if a certain condition is true. You
dont need to wrap each statement in its own if clause. Instead, you can use curly braces to wrap
the code you want to be executed conditionally.
You can even take it one step further and nest if statements inside of other if statements.
If Else Statements
In some cases you want to execute a piece of code when a condition is met and a different piece
of code if that condition is not met. In that case, you would have to use and if-else statement. else
extends an if statement to execute an expression if the statement evaluates to FALSE. Here is an
example that extends the one above:
Keep in mind that as soon as a condition is evaluated to TRUE, the script executes the associated
block of code and breaks out of the if-elseif-else structure and will not go down to any of the
other conditions. That is, only one condition will be executed when using an if-elseif-else.A
structure that is related to the if-elseif-else structure is the switch statement which is the subject of
discussion next.
Switch
The switch statement is similar to a bunch of if statements operating on the same variable.
Imagine you have some variable that can take some values. In your code, you want to compare if
that variable corresponds to some specific values. For each value, there is a code segment that has
to be executed. Also, if the variable does not meet any of those cases, you want to execute some
default piece of code. You could do this using a long if-elseif-else structure, but you could also
do it using a switch statement.
A switch statement does the same thing as an if-else-if structure, but saves you some typing.
The syntax for a switch statement is:
You can have as many cases as you need for your code. An important thing to keep in mind is the
presence of the break statement. The break command breaks you out of the current structure or loop
you are in. If you did not have the break command inside each case, PHP would go through each
case. That is, PHP checks the variable against each case and executes the specified code within if
the comparison returns TRUE. If PHP does not encounter a break command, it continues to the
next case and executes the specific code if the comparison of that case returns TRUE.This is one
of the differences between using an if-elseif-else structure and a switch statement.
Lets take a look at an example comparison between the if-elseif-else structure and the switch
statement:
Thats pretty much all there is to switch statements. Learn how to utilize them and you will make
your life faster than having to write out all of the if-else statements.
OK, after that digression, lets see the alternate syntax for an if statement:
This looks exactly like the code above and will work exactly as you would expect it to.
Helpful Tip!
Whenever you are creating control structures that have to be encapsulated inside of curly braces,
always write out the entire control structure skeleton first before going in to fill it up. That is, if
you are creating an if statement, you would write out:
Always make sure you do this! If you do this, you will never have to keep track of opening and
closing brackets and/or parentheses. In fact, you should make it a rule to yourself. WHENEVER
YO U O PEN A BRACKET O R PAREN THES IS , C LO S E IT BEFO RE YO U WRITE ANY O THER C O D E. Then go back and fill
in the code you need. This will keep you from making stupid errors and spending precious time
debugging code while you could have been writing an amazing application.
Another way to write the alternate syntax is to keep the curly braces and break out of PHP like
normal. The following two statements are equivalent:
That does it for the alternate syntax for control structures. You will often find yourself these
alternate syntaxes in your code when you are juggling between PHP and HTML. In the next
subchapter we will begin out discussion of the different types of looping structures in PHP. Keep
in mind that the alternate syntax we discussed in this structure is applicable to some of the next
structures as well.
While
This is the first type of loop that we will be talking about and it is also the simplest. The form of a
while statement is:
The meaning of the while loop is also simple to understand. While loops tell PHP to execute the
nested statement (does not need to be a single statement, it could be a block of code and in most
cases it will be) as long as the while expression evaluates to TRUE. The expression is evaluated
once at
the beginning of the loop so even if the value of the expression is to change during the execution
of the loop, the execution of the loop does not terminate (unless told otherwise) until the beginning
of the next iteration. It is important to note that if the expression is FALSE the first time PHP
checks it, the statement will not be executed.
As we mentioned, the statement could be a number of statements wrapped inside of curly braces.
As with the if statement, we can use the alternate syntax to form our while loop. This is the
general form of a while loop using alternate syntax for control structures:
While loops are very commonly used, especially when you are extracting information from a
database
The while condition will always evaluate to TRUE (in essence, the loop reads: while TRUE, execute
{statement} ).
Do-while
The do-while loop is similar to the loop we saw in the previous subchapter, with a minor
difference. Do-while loops have the following structure:
This means that PHP will execute everything that is inside the do clause first, and then it will
check the condition. If it evaluates to TRUE, PHP will loop back. What you will notice is that
you evaluate the condition at the end of the loop instead of the beginning. This means that the
code inside the do statement is guaranteed to run at least once. For example:
This code will execute exactly one time before terminating because the truth expression
evaluates to FALSE.
The only major difference between a do-while and a while loop is that the do-while will run the
code at least once, whereas the while loop will not. With that being said, it should be noted that
the while loop is the loop that you will be using most commonly in practice, but dont forget that
the do-while loop exists!
For
For loops are more complex than while and do-while loops, but you should become really
comfortable with them because you will be using them all the time! Here is the basic syntax of a
for loop:
The first expression is evaluated (executed) once at the beginning of the loop.
The second expression is evaluated at the beginning of each iteration. If it evaluates to the TRUE,
the loop will continue, and the nested statement(s) are executed. If it evaluates to FALSE, the
execution of the loop terminates. At the end of each iteration, the third expression is evaluated
(executed).
Each of the expression can be empty or it can contain multiple expressions separated by
commas. If the second expression is empty, PHP assumes it to be TRUE. Essentially this will
create an infinite FOR loop. Lets look at the following examples:
As with the while and if statements, for loops support the alternate colon syntax for control
structures:
Very often, people will loop through arrays using for loops. While it is not wrong to do this, it is
more convenient to use foreach loops for arrays or objects, because that control structure is
specifically setup for arrays and provides you with more control.
Now that weve introduced the two types of looping in PHP, you may be asking yourself: What is
the difference between a while and a for loop and when should you use each one? Here is the
general rules of thumb that will guide you through the proper usage of for and while loops in your
PHP applications.
For loops are for when you know how many iterations of the loop you need. For example, if you
want to operate on each element of an array, you use a for loop that loops from the first to the last
value of the array. The easiest way is if you have an array where each key is an incremental
number. You initialize the counter variable to 0 (this is usually going to be the first value of the
array. Remember, PHP counts from 0). After that, you check whether you have reached the last
element of the array. To do that you check whether the counter variable is smaller than or equal to
the length of the array (you can use the count() function to find the number of elements in the array.)
The last expression will be incrementing the counter variable.
While loops on the other hand, are to be used when you dont know how many iterations your loop
will take until the truth condition evaluates to FALSE.
A closing word about for loops:
For loops can be used on string characters as well. Can you set up a for loop that will print out the
letters of the alphabet?
Foreach
The foreach construct provides an easy way to iterate over arrays and objects, but it will only
work on arrays and objects and will give you an error if you try to use a foreach on a different
kind of variable type. There are two syntaxes to the foreach loop:
The first syntax loops over an array ( array_expression )and takes the value of each element and assigns
it to $value . In the second syntax, you reference both the key and the value of each element of the
array. Lets look at an example:
Note that the reference to $value remains after the foreach loop, so you should unset it (destroy it)
after the loop to prevent any issues with your code.
Foreach loops and while loops are very commonly used in tandem when working with database
information, but we shall see that in the chapter concerning databases.
Break
So far we have seen the use of the break command to break out of loops, but we have not defined it
explicitly until now.
break
ends execution of the current for , foreach , while , do-while or switch structure.
accepts an optional numeric argument which tells it how many nested enclosing structures are
to be broken out of.
break
Here is an example of how the break command works with the optional argument:
Continue
The continue command is similar to the break command, but it does not break out of the loop.
Instead it continues to the next iteration of the loop. This command can be used inside of switch
statements as well, but the results of that would be the same using a break. You can think of it this
way:
Continue takes you to just before the last closing curly bracket of your structure. If it is a loop,
you will go to the next iteration of the loop. For a switch statement, when the execution is taken
to just before the closing curly bracket, you are essentially being taken to the end of the switch
statement.
In contrast, break will take you to just outside the last closing curly bracket of your code. For a
loop that means that you have broken out of the loop. For a switch statement, that means that you
break out of the switch statement and do not go through any of the cases.
As with the break statement, continue takes an optional argument that tells PHP how many
levels of enclosing loops it should skip.
Return
The return command returns program control to the calling module. Execution resumes at
the statement following the called modules invocation.
If you use return from within a function, the return statement immediately ends execution of the
current function, and returns the argument of the return as the value of the function call.
Try to abstain from using return inside of a file that is included in another file as this is bad
practice.
Include
Files are included based on the file path given or, if none is given, the include_path specified. If
the file isn't found in the include_path, include will finally check in the calling script's own
directory and the current working directory before failing. The include construct will emit a
warning if it cannot find a file; this is different behavior from require, which will emit a fatal
error.
If a path is defined whether absolute (starting with a drive letter or \ on Windows, or / on
Unix/Linux systems) or relative to the current directory (starting with . or ..) the include_path
will be ignored altogether. For example, if a filename begins with ../, the parser will look in the
parent directory to find the requested file.
For more information on how PHP handles including files and the include path, see the
documentation for include_path.
When a file is included, the code it contains inherits the variable scope of the line on which the
include occurs. Any variables available at that line in the calling file will be available within
the called file, from that point forward. However, all functions and classes defined in the
included file have the global scope.
Require
Require is identical to include, except that upon failure, it will return an error and execution of
the script will stop.
Require_once
This is similar to require, except require_once will check whether the file has been included up to
that point. If it has, it will not include it again. Hence, it will require the file to be included only
once.
Include_once
Works exactly like require_once, except it will not produce an error.
Conclusion
This is probably one of the most important chapters in the book. With the concepts and techniques
you learned in this chapter you should be able to fully utilize your PHP skills to create amazing
applications. The control structures introduced here are the basis of each PHP script you will
write and encounter. Combine these with the next chapters about functions and database
connections and usage and you will be well on your way to creating advanced and complicated
web applications.
Exercise 1
In this PHP exercise, you will use a conditional statement to determine what gets printed to the
browser. Write a script that gets the current month and prints one of the following responses,
depending on whether it's August or not:
It's August, so it's really hot.
Not August, so at least not in the peak of the heat.
Hint: the function to get the current month is ' date('F', time()) ' for the month's full name.
Exercise 2
In this PHP exercise, you will put all the loops through their paces. Write a script that will print
the following to the browser:
abc abc abc abc abc abc abc abc
abc
xyz xyz xyz xyz xyz xyz xyz xyz
xyz
123456789
1.
2.
3.
4.
5.
6.
Item A
Item B
Item C
Item D
Item E
Item F
Create the 'abc' row with a while loop, the 'xyz' row with a do-while loop, and the last two
sections with for loops. Remember to include HTML and source code line breaks in your output.
No arrays allowed in this solution.
Exercise 3
Loops are very useful in creating lists and tables. In this PHP exercise, you will use a loop to
create a list of equations for squares.
Using a for loop, write a script that will send to the browser a list of squares for the numbers 1-12.
Use the format, "1 * 1 = 1", and be sure to include code to print each formula on a different line.
The function name is defined by you. It is a good idea to give the function a name that reflects
what the function will do. The naming convention for functions is pretty much the same as the
naming convention for variables (functions can start with letters or underscores but not
numbers). An important thing to note is that function names are not case-sensitive. Therefore,
the following code will produce an error:
If you want to call the function you created, you just write the name of the function followed by a
pair of parentheses. If your function takes parameters, you put the corresponding variable or
variables in between the parentheses.
Function Arguments
Information can be passed to functions through arguments. An argument is just like a
variable. Arguments are specified after the function name, inside the parentheses. You can
add as many
arguments as you want, just separate them with a comma. Lets modify the previous example to
include a name:
Functions arguments can take default values. For example, if you call the function without
specifying an argument, the function will assume the default argument value;
Return values
Often times, you want the function to return a specific value. To do this, you use the return
statement:
Variable Scope
When dealing with variables, it is important to know what their scope is (or the context in which
they are defined). For example:
In this case, the variable $var can be used by any code that is included inside of functions.php . The
context of a variable, however, changes when we create our own functions.
Within user-defined functions a local function scope is introduced. Any variable used
inside a function is by default limited to the local function scope. For example:
This script will not produce any output because the echo statement refers to a local version of
the $a variable, and it has not been assigned a value within this scope. You may notice that this is a
little bit different from the C language in that global variables in C are automatically available to
functions unless specifically overridden by a local definition. This can cause some problems in
that people may inadvertently change a global variable. In PHP global variables must be declared
global inside a function if they are going to be used in that function.
Lets look at how to declare variables as global.
Pre-defined functions
PHP comes built with many pre-defined functions and this is what makes PHP so efficient and
useful. We cannot possible examine all of the PHP functions as this will take another book itself
(currently, the PHP documentation lists 9457 built in functions.). Lets start with the most basic
functions that can be used inside of PHP.
String functions
PHP comes with a wide range of functions for string manipulation.
Word count
PHP comes with a pre-built function that will count the number of words in a string for you. It is
of the following form:
The second argument of the function is optional and can take 3 values. Here are the supported
values
The arguments can be strings or arrays. If needle and replacement are arrays, then str_replace() takes a
value from each array and uses them to search and replace on haystack . If replacement has fewer
values than needle , then an empty string is used for the rest of replacement values. If needle is an
array
and replacement is a string, then this replacement string is used for every value of haystack . The
converse would not make sense, though.
There is an optional fourth argument that could be passed which will be set to the number of
replacements performed.
There is a modification to the str_replace function that will replace without regard to case. In
other words, it will do a case-insensitive replace. This function is called str_ireplace and takes
the same arguments.
String length
There are a number of functions in PHP that will return the length of a string. The simplest to
use is the following:
This function has an optional parameter which is the encoding of the string. For example, if you
want to set the encoding to UTF-8, you just provide the string UTF-8 . PHP supports numerous
encoding types which you can easily find by going to the official PHP manual.
Another way to count the number of characters is to use the iconv_strlen() function.It works very
much like the mb_strlen() function and takes the same parameters, except it is a lot stricter when it
comes to bad sequences inside your string.
The reason for the different functions is that they actually count different things. strlen() counts the
number of bytes inside of a string. Usually a character is equivalent to one byte, but that is not
always the case. This is why the other functions exist.
Sometimes you will find that you have a string that has unneeded whitespace at the end of it.To
get of whitespace on both sides of a string, you can use the trim() function.A useful feature of the
trim() function is that it can trim not only whitespace but any additional characters that you
specify.Alternatively, if you only want to trim from the left or the right, there are the ltrim() and
rtrim() functions.
number of characters. If the value is negative, the value specifies how many characters will be
omitted from the
If you want to find the number of times a string occurs inside another string you can use
the substr_count() function. This function takes two required parameters and two optional
parameters. Here is the syntax for the function and some examples on how to use it in your own
code:
The first parameter is the string to check. The second parameter is the string to search for. The
third parameter specifies where in the string to start searching. The third parameter specifies the
length of the search.
Another function that finds a substring inside a larger string is the strpos() function. This function
finds the position of the first occurrence of a string inside another string. Lets look at the syntax
for the function as well as a couple of different examples to understand it:
The first parameter is the string to check. The second parameter is the string to search for. The
third parameter specifies where in the string to start searching.
The other case functions that you might find useful arethe
example:
ucwords()
and the
ucfirst()
. Here is an
The first function is the implode() function. This function takes an array and joins the array
elements with a string. The syntax is the following:
The separator specifies what to be placed in between the array elements. Here is an
example:
This is a very powerful function that, if used properly, can save you from having to write out code
and cut down on execution time. Make sure to look at the exercises at the end of the chapter to see
an example of how this function can be used to optimized code performance.
Lets look at the other function. We saw how to implode an array into a string, now lets look at
how to explode a string into an array. The function is called explode() and takes two parameters.
The separator is the part of the string that you want to separate by and the string is the string you
are exploding. Here is an example:
Conclusion
Weve expanded our PHP knowledge even more in this chapter by covering the topics of
functions in PHP. We covered user-defined functions and a small amount of the pre-defined
functions in PHP. Here are some practice exercises to make you comfortable with functions.
Exercise 1
In chapter 2, exercises 2 and 3 you created an associative array that represented a menu and had
to output some string based on the contents of the array. We revisit this array in this exercise.
Using the same array, create a function that takes three parameters: the menu array, the category
selection and meal selection. The function has to output an HTML string. The string should be a
paragraph tag containing the content in the following format:
You
{name of me al he re (in
picked:
This
meal isbold)}.
made from: {list ingredients here (comma-separated
list)}.
The nutritional information for this meal is: {list nutritional information here (comma-separated
list)}.
What is an API?
An API (Application Programming Interface) defines the classes, methods,
functions and variables that your application will need to call in order to carry out
a desired task. In the case of PHP applications that need to communicate with
databases the necessary APIs are usually exposed via PHP extensions.
APIs can be procedural or object-oriented. With a procedural API you call
functions to carry out tasks, with the object-oriented API you instantiate classes
and then call methods on the resulting objects. Of the two the latter is usually
the preferred interface, as it is more modern and leads to better organized code.
We have not yet covered OOP (object-oriented programming) in PHP, so if you
are not familiar with the concept, you can skip to chapter 10 of this book and then
return to this chapter after you have gotten a hang of OOP!
There are several APIs available that will allow you to connect to the MySQL
server. We will have a discussion of the options and which to choose depending on
your application.
Connectors
MySQL provides documentation that explains all its terms. The term connector
refers to a piece of software that allows your application to connect to the MySQL
database server. MySQL has a variety of connectors depending on your language,
including connectors for PHP.
When your application requires a database, you need to write your code to perform
activities such as connecting to the database server, querying the database, updating
the database, removing entries and other database-related functions. Software is
required to provide the API that your PHP application will use, and also handle the
communication between your application and the database server, possibly using
Drivers
A driver is a piece of software designed to communicate with a specific type of
database server. The driver may also call a library, such as the MySQL Client
Library or the MySQL Native Driver. These libraries implement the low-level
protocol used to communicate with the MySQL database server.
By way of an example, the PHP Data Objects (PDO) database abstraction layer may
use one of several database-specific drivers. One of the drivers it has available is
the PDO MYSQL driver, which allows it to interface with the MySQL server.
Sometimes people use the terms connector and driver interchangeably, this can be
confusing. In the MySQL-related documentation the term "driver" is reserved for
software that provides the database- specific part of a connector package.
Extensions
In the PHP documentation you will come across another term - extension. The PHP
code consists of a core, with optional extensions to the core functionality. PHP's
MySQL-related extensions, such as the mysqli extension, and the mysql extension,
are implemented using the PHP extension framework.
An extension typically exposes an API to the PHP programmer, to allow its
facilities to be used programmatically. However, some extensions which use the
PHP extension framework do not expose an API to the PHP programmer.
The PDO MySQL driver extension, for example, does not expose an API to the PHP
programmer, but provides an interface to the PDO layer above it.
The terms API and extension should not be taken to mean the same thing, as an
extension may not necessarily expose an API to the programmer.
mysqli Extension
PDO Extension
PHP Data Objects, or PDO, is a database abstraction layer specifically for PHP
applications. PDO provides a consistent API for your PHP application regardless of
the type of database server your application will connect to. In theory, if you are
using the PDO API, you could switch the database server you used, from say
Firebird to MySQL, and only need to make minor changes to your PHP code.
Other examples of database abstraction layers include JDBC for Java applications
and DBI for Perl. While PDO has its advantages, such as a clean, simple, portable
API, its main disadvantage is that it
doesn't allow you to use all of the advanced features that are available in the latest
versions of
MySQL server. For example, PDO does not allow you to use MySQL's support for
Multiple
Statements.
Basics
MySQL is a database server that allows you to create and store multiple databases for use. A
database is a place where you store all of the data for a given application. A database consists of
multiple tables with entries. Each row of a table represents a database entry and each column of
that table represents some particular information. Below is a picture of the PHPMyAdmin
interface with a database open showing three tables inside:
MySQLi
Dual interface
The mysqli extension features a dual interface. It supports the procedural and objectoriented programming paradigm.
Users migrating from the old mysql extension may prefer the procedural interface. The procedural
interface is similar to that of the old mysql extension. In many cases, the function names differ only
by prefix. Some mysqli functions take a connection handle as their first argument, whereas
matching functions in the old mysql interface take it as an optional last argument.
In addition to the classical procedural interface, users can choose to use the object-oriented
interface. The documentation is organized using the object-oriented interface. The object-oriented
interface shows functions grouped by their purpose, making it easier to get started. The reference
section gives examples for both syntax variants.
There are no significant performance differences between the two interfaces. Users can base their
choice on personal preference.
Connections
The MySQL server supports the use of different transport layers for connections. Connections
use TCP/IP, Unix domain sockets or Windows named pipes.
The hostname localhost has a special meaning. It is bound to the use of Unix domain sockets. It is
not possible to open a TCP/IP connection using the hostname localhost you must use 127.0.0.1
instead.
Depending on the connection function used, assorted parameters can be omitted. If a parameter is
not provided, then the extension attempts to use the default values that are set in the PHP
configuration file.
The resulting parameter values are then passed to the client library that is used by the extension.
If the client library detects empty or unset parameters, then it may default to the library built-in
values.
Built-in connection library defaults
If the host value is unset or empty, then the client library will default to a Unix socket connection
on localhost. If socket is unset or empty, and a Unix socket connection is requested, then a
connection to the default socket on /tmp/mysql.sock is attempted.
On Windows systems, the host name . is interpreted by the client library as an attempt to open a
Windows named pipe based connection. In this case the socket parameter is interpreted as the
pipe
name. If not given or empty, then the socket (pipe name) defaults to \\.\pipe\MySQL.
If neither a Unix domain socket based not a Windows named pipe based connection is to
be established and the port parameter value is unset, the library will default to port 3306.
The mysqlnd library and the MySQL Client Library (libmysqlclient) implement the same logic
for determining defaults.
Connection options
Connection options are available to, for example, set init commands which are executed upon
connect, or for requesting use of a certain charset. Connection options must be set before a
network connection is established.
For setting a connection option, the connect operation has to be performed in three steps:
creating a connection handle with mysqli_init(), setting the requested options using
mysqli_options(), and establishing the network connection with mysqli_real_connect().
Executing statements
Statements can be executed with the mysqli_query(), mysqli_real_query() and mysqli_multi_query() functions.
The mysqli_query() function is the most common, and combines the executing statement with a
buffered fetch of its result set, if any, in one call. Calling mysqli_query() is identical to
calling mysqli_real_query() followed by mysqli_store_result() .
After statement execution results can be retrieved at once to be buffered by the client or by read
row by row. Client-side result set buffering allows the server to free resources associated with the
statement results as early as possible. Generally speaking, clients are slow consuming result sets.
Therefore, it is recommended to use buffered result sets. mysqli_query() combines statement
execution and result set buffering.
PHP applications can navigate freely through buffered results. Navigation is fast because the
result sets are held in client memory. Please, keep in mind that it is often easier to scale by client
than it is to scale the server.
Prepared statements
Prepare is followed by execute. During execute the client binds parameter values and sends them
to the server. The server creates a statement from the statement template and the bound values to
execute it using the previously created internal resources.
A prepared statement can be executed repeatedly. Upon every execution the current value of the
bound variable is evaluated and sent to the server. The statement is not parsed again. The
statement
Every prepared statement occupies server resources. Statements should be closed explicitly
immediately after use. If not done explicitly, the statement will be closed when the statement
handle is freed by PHP.
Using a prepared statement is not always the most efficient way of executing a statement. A
prepared statement executed only once causes more client-server round-trips than a non-prepared
statement. This is why the SELECT is not run as a prepared statement above.
Conclusion
In this chapter we introduced some concepts related to creating databases and handling
database connections using the MySQLi API in PHP. You should now be able to create your
own databases using the PHPMyAdmin interface and connect to them with PHP.
There is nothing special about this form. It is a straight HTML form with no special tags of any
kind. When the user fills in this form and hits the submit button, the action.php page is called.
Alternatively, you could leave the action parameter empty and the information from the page will
be sent to the same page you are on. (Essentially, it will refresh the page and make the form data
available to you).
Security
Important!!
Anytime you are dealing with user submitted data, you must assume that the user using your site
has bad intentions. Always assume that you are writing a site that someone is trying to hack.
Never trust that your users are not going to exploit a vulnerability in your site. Its better to be
overly secure than to not be secure.
When it comes to form data, you have to always be cautious. Think of it this way. When you give
a user a field for them to type their name, who will guarantee that they will type their name in that
field? What happens if they type some PHP code? Or maybe an SQL query? Or maybe some
JavaScript? You have to be the one that makes sure you prevent XSS (cross-site scripting) attacks
and SQL injection. Those are the two most common types of web hacking and will cause you
intense headaches if you dont think ahead of time.
Think of the form from the previous example. When the user fills in this form and hits the submit
button, the action.php page is called. In this file you would write something like this:
Apart from the htmlspecialchars() and (int) parts, it should be obvious what this does. htmlspecialchars()
makes sure any characters that are special in html are properly encoded so people can't inject
HTML tags or Javascript into your page. For the age field, since we know it is a number, we can
just convert it to an integer which will automatically get rid of any stray characters. You can also
have PHP do this for you automatically by using the filter extension. The $_POST['name'] and
$_POST['age'] variables are automatically set for you by PHP. Aabove we just introduced the $_POST
superglobal which contains all POST data. Notice how the method of our form is POST. If we
used the method GET then our form information would live in the $_GET superglobal instead.
Form validation is a wide topic that we will not cover in this book, but hopefully this example
gives you an idea of what to do. Generally the page that is your form processing page, you will
have a number of if statements that check a number of things. Here is a general outline of what you
should do when validating a form:
-
If a field is important, meaning that is must have a value (e.g. a password field for a user
registering to your site) you have to check whether that value is set. For example, use
something like: if(!isset($_POST[password])) {do sth here};
Generally it is a good idea to create an errors array. This array will contain all of the errors
that occur with the form. When you validate and a validation of a field fails, you add an error.
At the end, you display these errors to your page so the user knows what they did wrong
Conclusion
In this brief chapter we covered some important concepts about dealing with form data and
security issues. Hopefully you have understood the importance of security in your web
applications.
Exercise 1
In the next PHP exercise, you will request input from the user, then move the user's response from
one file to another and do something with it.
Create two separate files. The first will contain a form with one input field asking for the user's
favorite city. Use the post method for the form. Although this file contains no PHP code, on my
localhost, it needs the .php extension to successfully call the second file.
The second file will contain PHP code to process the user's response. (In this case, something very
simple.) After the user clicks the submit button, echo back Your favorite city is $city., where $city
is the input from the form.
Hint: the variable that contains the user's input is an array. Arrays will be addressed in
future exercises, but this particular array needs to come into play here. The array variable
is
$_POST['name'], where 'name' is the name of your input field.
Exercise 2
One very useful thing you can do with PHP is include the request for user input and the
response in the same file, using conditional statements to tell PHP which one to show. For this
PHP exercise, rewrite the two files of the previous exercise into one file using an if-else
conditional statement.
Hint: You'll need some way to tell if the form has been submitted. The function to determine
if a variable has been set and is not null is isset().
Exercise 3
For this PHP exercise, you will use the same format as the previous exercise, requesting input in
the first part, and responding in the second, through the magic of PHP's if-else statement. In the
first section, give the user an input field and request that they enter a day of the week.
For the second section, you'll need the following poem:
Laugh on Monday, laugh for danger.
Laugh on Tuesday, kiss a stranger.
Laugh on Wednesday, laugh for a
letter. Laugh on Thursday, something
better. Laugh on Friday, laugh for
sorrow. Laugh on Saturday, joy
tomorrow.
Using the else-elseif-else construction, set each line to output in response to the day the user inputs,
with a general response for any input that is not in the poem.
Sessions
Sessions are a simple way to store data for individual users against a unique session ID. This can
be used to persist state information between page requests. Session IDs are normally sent to the
browser via session cookies and the ID is used to retrieve existing session data. The absence of an
ID or session cookie lets PHP know to create a new session, and generate a new session ID.
Sessions follow a simple workflow. When a session is started, PHP will either retrieve an existing
session using the ID passed (usually from a session cookie) or if no session is passed it will create
a new session. PHP will populate the $_SESSION superglobal with any session data after the session
has started. When PHP shuts down, it will automatically take the contents of the $_SESSION
superglobal, serialize it, and send it for storage using the session save handler.
By default, PHP uses the internal files save handler which is set by session.save_handler . This saves
session data on the server at the location specified by the session.save_path configuration directive.
Sessions can be started manually using the session_start() function. If the session.auto_start directive is set
to 1, a session will automatically start on request startup.
Sessions normally shutdown automatically when PHP is finished executing a script, but can be
manually shutdown using the session_write_close() function.
Caution
Do NOT unset the whole $_SESSION with unset($_SESSION) as this will disable the registering of
session variables through the $_SESSION superglobal.
the $_SESSION superglobal and pass this along with the session ID to the write callback. After the
write
callback has finished, PHP will internally invoke the close callback handler.
When a session is specifically destroyed, PHP will call the destroy handler with the session ID.
PHP will call the gc callback from time to time to expire any session records according to the set
max lifetime of a session. This routine should delete all records from persistent storage which
were last accessed longer than the $lifetime .
Cookies
PHP transparently supports HTTP cookies. Cookies are a mechanism for storing data in the
remote browser and thus tracking or identifying return users. You can set cookies using the
setcookie() or setrawcookie() function. Cookies are part of the HTTP header, so setcookie() must
be called before any output is sent to the browser. This is the same limitation that header() has.
You can use the output buffering functions to delay the script output until you have decided
whether or not to set any cookies or send any headers.
Any cookies sent to you from the client will automatically be included into a $_COOKIE autoglobal array if variables_order contains "C". If you wish to assign multiple values to a single
cookie, just add [] to the cookie name.
Depending on register_globals, regular PHP variables can be created from cookies. However it's
not recommended to rely on them as this feature is often turned off for the sake of security.
For more details, including notes on browser bugs, see the setcookie() and setrawcookie()
function. A cookie is created with the setcookie() function.
Conclusion
In this chapter we covered creating and destroying Cookies and Sessions in PHP
Introduction
File handling is an important part of all web applications. You will sometimes find it necessary
to open and process a file or files for different reasons and tasks. With that being said, we must
issue a word of caution. When you are manipulating files you must be very careful. You can do a
lot of damage if you do something wrong. Common errors are: editing the wrong file, filling a
hard-drive with garbage data, and deleting the content of a file by accident.
File Handling
Reading Files
The readfile() function reads a file and writes it to the output buffer.
Assume we have a text file called "webdictionary.txt", stored on the server, that looks like this:
The PHP code to read the file and write it to the output buffer is as follows (the readfile()
function returns the number of bytes read on success):
Opening files
A better method to open files is with the fopen() function. This function gives you more options
than the readfile() function.
We will use the text file, "webdictionary.txt", during the lessons:
The first parameter of fopen() contains the name of the file to be opened and the second parameter
specifies in which mode the file should be opened. The following example also generates a
message if the fopen() function is unable to open the specified file:
Modes
Description
r
Open a file for read only.
File pointer starts at the
beginning of the file
w
Open a file for write only.
r+
a+
x+
read/write. Returns
FALSE and an error if file
already exists
Notice that we wrote to the file "newfile.txt" twice. Each time we wrote to the file we sent the
string
$txt that first contained "John Doe" and second contained "Jane Doe". After we finished writing,
we closed the file using the fclose() function.
PHP Overwriting
Now that "newfile.txt" contains some data we can show what happens when we open an existing
file for writing. All the existing data will be ERASED and we start with an empty file.
In the example below we open our existing file "newfile.txt", and write some new data into it:
If we now open the "newfile.txt" file, both John and Jane have vanished, and only the data we
just wrote is present:
Conclusion
With that we conclude our discussion of PHP File Handling. You would find file handling to be
useful if you want to store information on your server, but you dont need a construct like a
database because you are storing a small amount of information that will not change frequently,
for example. There are many other examples for when File Handling would be useful, but we
shall not go into analyzing those.
Basics
Before you can get too deep into the finer points of OOP, a basic understanding of the differences
between objects and classes is necessary. This section will go over the building blocks of classes,
their different capabilities, and some of their uses.
Right off the bat, there's confusion in OOP: seasoned developers start talking about objects and
classes, and they appear to be interchangeable terms. This is not the case, however, though the
difference can be tough to wrap your head around at first.
A class, for example, is like a blueprint for a house. It defines the shape of the house on paper,
with relationships between the different parts of the house clearly defined and planned out, even
though the house doesn't exist.
An object, then, is like the actual house built according to that blueprint. The data stored in the
object is like the wood, wires, and concrete that compose the house: without being assembled
according to the blueprint, it's just a pile of stuff. However, when it all comes together, it becomes
an organized, useful house.
Classes form the structure of data and actions and use that information to build objects. More than
one object can be built from the same class at the same time, each one independent of the others.
Continuing with our construction analogy, it's similar to the way an entire subdivision can be built
from the same blueprint: 150 different houses that all look the same but have different families
and decorations inside.
Class
Basic class definitions begin with the keyword class, followed by a class name, followed by a pair
of curly braces which enclose the definitions of the properties and methods belonging to the class.
The class name can be any valid label, provided it is not a PHP reserved word. A valid class
name starts with a letter or underscore, followed by any number of letters, numbers, or
underscores. As a regular expression, it would be expressed thus: ^[a-zA-Z_\x7f-\xff][a-zA-Z09_\x7f-\xff]*$.
A class may contain its own constants, variables (called "properties"), and functions (called
"methods").
The pseudo-variable $this is available when a method is called from within an object context. $this
is a reference to the calling object (usually the object to which the method belongs, but possibly
another object, if the method is called statically from the context of a secondary object).
<?php
clas! A
{
f unctio foo ()
{
i1 ( isse( $this )) {
ech< '$this is defined (';
ech< get_class ($this);
ech< ")\n" ;
} els {
ech< "\$this is not defined.\n" ;
}
}
} II continued on next page
clas! B
{
f unctio bar()
{
If Note: the next line will issue a warning if
E_STRICT is enabled.
A::foo ();
}
}
$a = ne"' A();
$a->foo ();
R..hAr ll
New
To create an instance of a class, the new keyword must be used. An object will always be created
unless the object has a constructor defined that throws an exception on error. Classes should be
defined before instantiation (and in some cases this is a requirement).
If a string containing the name of a class is used with new, a new instance of that class will
be created. If the class is in a namespace, its fully qualified name must be used when doing
this.
In the class context, it is possible to create a new object by new self and new parent.
When assigning an already created instance of a class to a new variable, the new variable will
access
the same instance as the object that was assigned. This behaviour is the same when passing
instances to a function. A copy of an already created object can be made by cloning it.
Extends
A class can inherit the methods and properties of another class by using the keyword extends in
the class declaration. It is not possible to extend multiple classes; a class can only inherit from
one base class.
The inherited methods and properties can be overridden by redeclaring them with the same name
defined in the parent class. However, if the parent class has defined a method as final, that method
may not be overridden. It is possible to access the overridden methods or static properties by
referencing them with parent::.
When overriding methods, the parameter signature should remain the same or PHP will generate an
E_STRICT level error. This does not apply to the constructor, which allows overriding with
different parameters.
Properties
Class member variables are called "properties". You may also see them referred to using other
terms such as "attributes" or "fields", but for the purposes of this reference we will use
"properties". They are defined by using one of the keywords public, protected, or private,
followed by a normal variable declaration. This declaration may include an initialization, but this
initialization must be a constant value--that is, it must be able to be evaluated at compile time and
must not depend on run-time information in order to be evaluated.
Within class methods non-static properties may be accessed by using -> (Object Operator): $this>property (where property is the name of the property). Static properties are accessed by using
the :: (Double Colon): self::$property.
The pseudo-variable $this is available inside any class method when that method is called from
within an object context. $this is a reference to the calling object (usually the object to which the
method belongs, but possibly another object, if the method is called statically from the context of
a secondary object).
Constants
It is possible to define constant values on a per-class basis remaining the same and unchangeable.
Constants differ from normal variables in that you don't use the $ symbol to declare or use them.
The value must be a constant expression, not (for example) a variable, a property, a result of a
mathematical operation, or a function call.
As of PHP 5.3.0, it's possible to reference the class using a variable. The variable's value cannot
be a keyword (e.g. self, parent and static). Lets look at some examples:
Autoloading Classes
Many developers writing object-oriented applications create one PHP source file per class
definition. One of the biggest annoyances is having to write a long list of needed includes at
the beginning of each script (one for each class).
A good way to avoid having to write multiple includes is to use the spl_autoload_register() function.
Here is an example:
In the example, above, "MyClass" is the name of the class that you are trying to instantiate, PHP
passes this name as a string to spl_autoload_register(), which allows you to pick up the variable
and use it to "include" the appropriate class/file. As a result, you don't specifically need to include
that class via an include/require statement...
Just simply call the class you want to instantiate like in the example above, and since you
registered a function (via spl_autoload_register()) of your own that will figure out where all your
class are located, PHP will use that function.
PHP 5 introduces a destructor concept similar to that of other object-oriented languages, such as
C++. The destructor method will be called as soon as there are no other references to a particular
object,
or in any order during the shutdown sequence.
Like constructors, parent destructors will not be called implicitly by the engine. In order to run a
parent destructor, one would have to explicitly call parent::__destruct() in the destructor body. Also
like constructors, a child class may inherit the parent's destructor if it does not implement one
itself.
The destructor will be called even if script execution is stopped using exit() . Calling exit()
in a destructor will prevent the remaining shutdown routines from executing.
Object Inheritance
Inheritance is a well-established programming principle, and PHP makes use of this principle in
its object model. This principle will affect the way many classes and objects relate to one another.
For example, when you extend a class, the subclass inherits all of the public and protected methods
from the parent class. Unless a class overrides those methods, they will retain their original
functionality.
This is useful for defining and abstracting functionality, and permits the implementation of
additional functionality in similar objects without the need to reimplement all of the shared
functionality.
Three special keywords self, parent and static are used to access properties or methods from
inside the class definition.
When an extending class overrides the parents definition of a method, PHP will not call the
parent's method. It's up to the extended class on whether or not the parent's method is called. This
also applies to Constructors and Destructors, Overloading, and Magic method definitions.
<?php
clas! MyCiass
{
protecte functio myFunc() {
echc "MyCiass::myFunc{)\n" ;
}
}
cl.as! OtherCiass extend MyCiass
{
II Override parent's definition
publi functio myFunc()
{
II But still call the parent function
paren::myFunc();
echc "OtherCiass::myFunc{)\n" ;
}
}
$class
ne111 OtherCiass();
$class ->myFunc();
?>
Conclusion
Lets look at a few exercises for Classes and Objects before we close this book.
Exercise 1
In this PHP exercise, you will build the beginnings of a user registration form. To do this, you will
create a class for making the select field, then use an object derived from the class in the form.
First of all, write an array that includes browser types: Firefox, Chrome, Internet Explorer, Safari,
Opera, Other.
Then begin to write the class Select. You will need two properties, $name for the name of the select
field, and $value , an array to provide the option values. You will also need four methods in
addition to the two methods you will adapt: setName, getName, setValue, getValue. Checking to
be sure the value is an array belongs in the setValue method, so write that here, and delete it from
makeSelect .
Now we come to the two functions you wrote to generate the select field. Change the
makeOptions function to iterate over the array argument's values rather than keys. This will be
your fifth method. Then revise the makeSelect function to be the sixth method in your class.
Next comes the HTML. Write a user registration form asking for name, username, email,
browser. Use text fields to collect the user data for the first three, then instantiate an object based
on your class for the select field. When the user clicks the submit button, return the data as
confirmation.
If you were creating a registration form to use on the Web, you would want to collect the data in a
database. However, using PHP with mySQL or other databases is beyond the scope of this
website.
Exercise 2
In the last PHP exercise, the Select class may have seemed like an awful lot of code to write for a
simple select field. The real value of classes and objects doesn't become apparent until you have
reason to reuse the code. So this time, you will expand your user registration form to use several
select fields.
Assume that you have good reason to need data about your users' browsing capabilities. Either you
want to tune your site, the content of your site concerns these issues, or something similar. Using
your select class, you can reuse the class code as often as you like to create select fields.
To build this new version of the registration form, start with the script you wrote for Classes Ex.
#1. Add the value of None as the first value in the $browsers array. Write two more arrays:
$speeds, including values Unknown, DSL, T1, Cable, Dialup, Other; and $os, including Windows,
Linux, Macintosh, Other. (Of course, these could be screen resolution or flash version or any other
relevant capability.)
You want data for how the user browses both at home and work. Above the browser select field,
add the subheading Work Access, and rename the browser label Primary Browser. (We all know
that many people use more than one.) Below that, add labels and select field objects for
Connection
Speed and Operating System. Next, add the subheading Home Access, with three new select
fields corresponding to the ones you created for Work Access.
Since you are using so many objects in this script, it's a good idea to destroy each one after it
has done its work. This will free up the memory the object occupied.
When the user hits the submit button, return the user's select field choices in two bulleted lists
under the same headings (Work Access, Home Access).
Exercise 3
If you completed PHP Classes Ex. #1 and #2, you have now written a working user registration
form. Time to tweak it and make it better.
First of all, it would be preferable to have the message --Select one-- at the top of each select
field. Add a line to the makeSelect() method to accomplish this. The value should be No
response. You won't need the "None" value at the top of the $browsers array, so delete that. With
this change to class Select, you can see how using a class can simplify your work. One line of
code, and all the select fields update.
Your user responses won't be very useful without some basic information, so the next task is to
make three of the fields required. Above the form, add * Indicates required field. Then add an
asterisk to the Name, Username, and Email fields.
Next, add code to validate the data in those three fields. This code will appear in the second half
of the script, after you have retrieved data from the $_POST[] variable. The function empty() will
let you know if there is data in the field. To help the user supply missing information, include a
back button with the error message. (If you completed Forms Ex. #3, you have already written
one of those.)
The email field is a special case. Not only can you check for the presence of data, you can check
for an @(at symbol), which would be included in any valid email address. So here the data must
satify two conditions to be acceptable. You can use the strpos() function to confirm the presence
of the @ character.
Congratulations! You did it. This was the last chapter in our PHP course. You should now be able
to create your own applications and tackle all sorts of projects. We hope you enjoyed this book as
much as we did writing it. Hopefully you found it useful and you took away some useful concepts
and techniques.
Answers to Exercises
These are the answers to exercises after each chapter. Keep in mind that many of these exercises
can be solved in different ways, so your code does not need to be exactly like the one provided
below to work properly. In many cases the code provided is just the general template from you
can develop your own code for the exercise.
Chapter 2
Exercise I
<?php
1/ Solution to CH 2, EX 1
$user_data = arra\(
username' => 'test user ,
'name'
=> arra\C
'first' => 'john' ,
last' => 'Doe ,
)'
'admin' => 0,
1User_id' => '42' ,
);
Ill.
II Concatenates the first name, a space and the last name.
$name = $user_data ['name' ]['first' ].' ' .$user_data ['name' ][ 'last' ];
//2.
/I a .
ilb.
//3
?>
Exercise
<?php
1/ Solution to CH 2, EX 2
$menu = arra\(
'catl' => arra\(
'meal1' => arra\(
'ingredients' => arra\('inqredient 1', 'inqredient 2', 'inqredient 3'),
'nutritional' => arra\('nutrientl: 50g' ,'nutrient2: 40g' , 'nutrient3: 20g' ),
),
'meal2' => arra\(
'ingredients' => arra\('ingredient 1', 'ingredient 2', 'ingredient 3'),
'nutritional' => arra\('nutrientl: 32g' ,'nutrient2: 42g' , 'nutrient3: 21g' ),
),
'meal3' => arra\(
'ingredients' => arra\('ingredient 1', 'ingredient 2', 'ingredient 3'),
'nutritional' => arra\('nutrientl: 53g' ,'nutrient2: 13g' , 'nutrient3: 6g'),
),
'meal4' => arra\(
'ingredients' => arra\('ingredient 1', 'ingredient 2', 'ingredient 3'),
'nutritional' => arra\('nutrientl: 78g' ,'nutrient2: 12g' , 'nutrient3: Og' ),
),
)'
);
?>
Exercise3
<?php
1/ Solution to CH 2, EX 3
$cat = 'catl' ;
$meal = 'meal2' ;
echc $msg = "You picked: $mea <br{>
Chapter 3
Exercise I
< html>
< head>
< meta http-equiv = " content type'tontent = " text/html;charset iso 8859 P
<title> Arithm etic AssignmerOperators:ttitle>
</head>
< body>
< ?php
$num = 8;
ech c "Value is now $nurr.< br/> " ;
$num += 2;
echc "Add 2. Value is now $nurr. < br/> " ;
$num -= 4;
echc "Subtra ct 4. Value is now $nurr. < br/> ";
$num *= 5;
ech c "Multiply by 5. Value is now $nurr. < br/> " ;
$num /= 3;
echc "Divide by 3. Value is now $nurr. < br/> " ;
$num ++;
echc "Increment value by one. Value is now $nurr.< br/> " ;
$num --;
echc "Decrement value by one. Value is now $nurr." ;
?>
</body>
</html>
Exercise
< html>
< head >
< meta http-equiv =" content type'tontent =" t ext/html;ch arset iso 8859 P
<title> Concatenatioro f StringS(/title>
</head >
< body>
< ?php
$around = 11 around 11 ;
ech c 'What goes ' . $around . ' comes ' . $around
?>
</bod y>
</html>
''
Exercise3
< html>
< head >
< meta http-equiv =" content type'tontent =" t ext/html;ch arset iso 8859 P
<title> VariableOataTypes<ttitle>
</head>
< body>
< h2> PHPVariableData Types</h2>
< p>
< ?php
$What sit = 'George' ;
echc "Value is ".gettype ($Whatsit ).".<br/>\n" ;
$What sit = 88.9 ;
echc "Value is ".gettype ($Whatsit ).".< br/>\n" ;
$Whatsit = true;
echc "Value is ".gettype ($What sit ).".< br/>\n" ;
$What sit = 8;
echc "Value is ".gettype ($Whatsit ).".<br/>\n" ;
$What sit = null;
echc "Value is ".gettype ($Whatsit ).".<br/>\n" ;
?>
</p>
</body>
</html>
Chapter 4
Exercise I
?>
</body>
</html>
Exercise Z
< html>
< head >
< m eta http-equiv = " content type'tontent = "textthtml;charset:i so-8859;1>
< title> Simpleloop s::ttitl e>
</head>
<body>
< h2> Simpleloop s::/h2>
<?php
echc "<p>\n";
$Counter = 1;
whild$Counter < 10){
echc 'abc ';
$Counter++;
}
echc
echc
<tp>\n ;
<p>\n.. ;
$count er = 1;
de{
echc 'xyz ';
$counter +
+;
} whilE ($count er < 10) ;
echc "</p>\n" ;
fo1 ($X= l; $X< l0; $X++ ){
echc 11 $X ";
}
echc "\n</ol> ;
//Note that letters may be used in the for loop in place of numbers .
?>
</body>
<thtml>
Exercise3
< html >
< head>
< meta http-equiv = "cont ent-type'tontent = "text/html;charset:iso 8859 P
<title> Squaresforthe Numb ersl-12</title>
</head>
< body>
< h2> Squaresforthe Numbersll2</h2>
< ?php
fo1 ($X = l; $X < = l2; $X++){
$result = $X * $X;
echc "$x *$X= $resul < br />\n" ;
}
?>
</body>
</html>
Chapter 5
Exercise I
< ?php
II Solution to CH 2, EX 2
$menu = arra)(
'catl' = > arra)(
'meall' = > arra)(
'ingredients' = > arra)('ingredient l', 'ingredient 2', 'ingredient 3'),
'nutritional' = > arra)('nutrientl: 50g', 'nutrient2: 40g', 'nutrient3: 20g' ),
),
I*
*I
?>
Exercise
2
< html>
< head>
< meta http-equiv = "cont ent-type'tontent = "text/html;charset:iso 8859 P
<title> RectangleArea Function::ttitle>
</head>
< body>
< h2> RectangleAreaFunctio n::th2>
< ?php
//Define function.
functio recArea ($1, $W ){
$area = $1 * $W ;
echc "A rectangle of length $1 and width $W has an area of $are<." ;
}
//Call function.
recArea (2, 4);
?>
</body>
< ?php
//Create array.
$months = arra\(
'january' = > 31.
'February' = > '28 days, if leap year 29' ,
'March' = > 31.
'April' = > 30,
'May' = > 3L
'June' = > 30,
'july = > 31,
'August' = > 31.
'September' = > 30,
'October' = > 31.
'November' = > 30,
'December' = > 31
);
//Define function . Use built in string function to make each option upper case.
//Don't forget to escape the quotes in your HTML.
functio1 option ($str){
echc "<option value=\" $stl\">" .ucfirst ($str ). "</option>\n" ;
}
?>
< html>
< head>
< meta httpequiv = "content-type'tontent = "text/html;charset:iso-8859 ;1>
< title> Days in a Mont ttitle>
</head>
< body>
< h2> Days in a Mont /h2>
< ?php
//If form not submitt ed, show form .
i t(!issei ($_POST['submit' ])) {
?>
<form method = "post"action = "yourfile.phpi'
< p> Pleasechoosea month;</p>
<select name= "month'>
< ?php
//Create options using the array and the function.
foreacl ($months as $k = > $V) {
option ($k );
}
?>
</select>
< p />
< input type = "submit"name= "submit"value = "Go"/>
EXercueJ-_----------------------------------------------------------
< ?php
//If form submitted , respond to user .
} else {
}
}
?>
<}body>
</html>
Chapter 7
Exercise 1
Exercise
2
< html>
< head>
< meta http-equiv = "content type'tontent = "text/html;charset:iso-8859;1>
<title> FormResponseNithlfEiseStatemen t:jt itle>
</head>
< body>
< h2> FavoriteCity</h2>
< ?php
//If form not submitt ed, display form.
il (!isse1($_POST ('submit' Jl) {
?>
<form method = "post"action = "you rfile.php!'
< !--Make sure you have entered the name you gave the file as the action .-->
Pleaseenteryourfav oritecity: < br />
< input type = "text"name= "city"/>
< p />
< input type = "submit"name = "submit'value = "Go"/>
</f orm>
< ?php
//If form submitt ed, process input .
}else{
//Retrieve string from form submission .
$city = $ POST ['city' ];
echc "Your favorite city is $cit .";
}
?>
</body>
</html>
Exercise3
< p />
< input type = "submit"name = "submit'value = "Go"/>
</form>
<?php
//If form submitted, process input .
}elsE {
//Retrieve string from post submission
$day = $_POST["day" ];
il ($day = = 'Monday' ){
ech< "Laugh on Monday, laugh for danger." ;
} elsei ($day == 'Tuesday' ){
ech< "Laugh on Tuesday, kiss a stranger." ;
} elsei ($day == 'Wednesday' ){
ech< "Laugh on Wednesday, laugh for a letter." ;
} elsei ($day== 'Thursday' ){
ech< "Laugh on Thursday, something better." ;
} elsei ($day == 'Friday' ){
ech< "Laugh on Friday, laugh for sorrow." ;
} elsei ($day == 'Saturday' ){
ech< "Laugh on Saturday, joy tomorrow ." ;
} elsE {
ech< "No information for that day." ;
}
}
?>
Chapter 10
< ?php
//Create array.
$browsers = arra}(
11
11
Firefox" ,
Chromel!
"Internet Explorer" ,
usafarill
"Opera" ,
"Other"
);
clas!Select {
//Property
private $name; //String variable.
privat $Value; //Array variable.
//Methods
/{fhe string set by this method will be the name of the select field .
publi functioa setNam e($name){
$this-> name =$name;
}
EXercnel'-------------------------------------------------------/
}//end class
?>
< ' DOCTYPE
html PUBLIC
"-/{W3C//DTD
XHTML 1.0
Transitionai7/EN]"
"bllR'ftwww
.w3,orgJI..RJxlitml
llQIJ2f)(bt m l l.tra
H:.:
J nsitional.dtd
< ?php
//If form submitt ed, process input.
}elsE {
//Could include code to send data to database here.
//Retrieve user responses.
$name=$ POST['name' ];
$usernam e = $ POST['username' ];
$email =$ POST['email' ];
//The following variable has an altered name to avoid confu sion.
$se1Browser =$ POST ['browser' ];
//Confirm responses to user.
echc "The following data has been saved for $namE: < br /> ";
echc "Usernam e: $usernam<br /> ";
echc "Email: $emai<br /> ";
echc "Browser: $se1Browse<br /> ";
}
?>
</body>
</html>
Exercise Z
<?php
//Create arrays .
$browsers = a rra}(
"None,
11
Firefox1 1 ,
"Chrome,
'Internet Explorer" ,
safan,
opera,
other'
);
$Speeds= arra}(
unknown ,
osL,
r1,
cable,
"Dialup,
"Other"
);
$os= arra}(
"Windows"
"Linux ,
Macintosh.. ,
"Other"
);
clas!Select {
//Properti es
privat $name; //String variable.
privat $Value; //Array variable.
//Methods
/{The string set by this method will be the name of the select field.
p u b l i fu n cti oo setName ($name ){
$this -> name = $name ;
}
p u blifun ct i oo getNameO{
ret u rr $this -> name;
}
f(on
}//end class
?>
E5]I DOCTYPE html PUBLI C " /{W3C//DTD XHTML 1.0
Transitional/lEN " "h!.f p_jw3.orglJ..F..\/XIltmll/DTP/Xbt m lltransitional.dtd
< html xmlns = "http://www .w3.org/1999/xhtmll'nl:lang= "en" lang = "en">
< head>
< meta http -equiv = "contenttype'tontent = "textthtml;charset:iso-8859;1>
<title> Multiple5ele ctobject s:ttitle>
</head>
< body>
< h2> UserRegistratio Fibr /></h2>
<?php
//If form not submitt ed, display form .
i t(!isse ($_POST['submit' ])){
?>
<form method = "post"action = "your file.php!'
< p> Name:<br />
< input type = " text" name = 11 name''size = " 60" /> </p>
< p> Username br />
< input type = "text" name = "username "size = " 60" /> </p>
< p> Email:<br />
<input type = 11 text name=" en1ail" size =" 60" /> </p>
<p> PrimaryBrowser:
< ?php
//Instantiat e object.
$browserWo rk = new Select ();
//Set properties.
$browserWork -> setName('browserWo rk' );
$browserWork -> setValue ($browsers );
/{The obj ect has the data it needs from the preceding commands.
{{Tell it to make the select field .
$browserWork -> makeselect ();
//Destroy the object.
unse1($browserWork );
echc "</p>\n<p>Co nnection Speed : ";
$speedWork = nev. Select ();
$speedWork -> setName('speedWork' );
$speedWork -> setValue ($speeds );
$speedWork -> makeselect ();
unse1($speedWork );
echc "</p>\n<p>Ope rating System : ";
$osWork = new Select ();
$osWork -> setName('osWork' );
$osWork -> setValue ($os);
$osWork -> makeselect ();
unse.1($osWork );
?>
</p>
< p> < strong> HomeAcces S::/strong> </p>
< p> PrimaryBrowser:
< ?php
$browserHome = new Select ();
$browserHome -> setName('browserHome' );
$browserHome -> setValue ($browsers );
$browserHome -> makeselect ();
unse1($browserHome );
echc "</p>\n<p>Co nnection Speed : ";
$speedHome = new Select ();
$speedHome -> setName('speedHome' );
$speedHome -> setValue ($speeds );
$speedHome -> makeselect ();
unse1($speedHome );
echc "</p>\n<p>Ope rating System : ";
$osHome = new Select ();
$osHome-> setName('osHome' );
?>
</body>
</html>
< ?php
//Create array.
$browsers = arra}(
"Firefox" ,
"Chrome".
"Internet Explorer" ,
"Safan",
opera ,
);
other"
$Speeds = arra}(
unknown .
osL,
-rl,
cable.
"D1alup
other"
);
$os= arra}(
"Windows"
"Linux" ,
"Macintosh"
, "Other"
);
clas!Select {
//Property
privat $name; //String variable.
privato $Value; //Array variable.
//Methods
trrhe string set by this method will be the name of the select field.
p ubli fun cti 01 setName($name){
$this-> name = $name ;
}
EXerc eJ
'-------------------------------------------------------
}//end class
?>
-slDOCTYPE html PUBLIC " //W3C//DTD XHTML 1.0 Transitional/LEN "
"b!! :ffw-ttw .w3.org{TR/Xht ml l/DTD/Xht mi l transitional.dtd "e>
< html xmlns = "http://www .w3.org/1999{xhtmll'nl:lang= "en" lang = "en">
< head>
< meta http-equiv = "content type'tontent = "text/html;charset iso 8859 ;J;!'
<title> FormMo difiedlnd valldateel:/title>
</head>
< bod y>
< h2> UserRegistratio n br /></h 2>
< ?php
//If form not submitted, display form .
i l(!isse ($_POST['submlt' j)) {
?>
* lndi catesrequire<field.
< form method = " p ost"action = "yo u rfll e.ph pll
<p> *Name :< br />
< input type = 'text name = 'na m esize= '60'/> </p>
<p>*Username<:br />
<input type = 'textname = userna m e"Size= '60'/></p>
<p> *Emaib<br />
<input type ='textname =emall'size='60'/><fp>
<p> <strong> WorkA cceslJ</strong> </p>
<p> PrimaryBrowser.
<?php
//Instant iate object .
$browse1Work = new Select ();
//Set propertie s.
$browse1Work > setNam e(browserWork );
$browse1Work > setValue ($browsers );
/{The object has the data it needs from the preceding commands .
/ fToll it tn n"'\ !,o tho
olol"t
iol
onc::Tr 1c-nccri\Ain.-t..- 1
$browserHome = $ POST['browserHome' ];
$speedHome = $_POST ['speedHome' ];
$osHome = $ POST['osHome' ];
//Check input.
il (empt1($name )) {
d i e('Error: Please enter your name. <br />
<input type="subm1t" name=back value="Back to form"
onclick = "self.locat1on=\'yourflle .php\"' J> <jbody> </html> );
}
?>
</body>
</html >
Conclusion
This book has found you because you have the ultimate potential.
It may be easy to think and feel that you are limited but the truth is you are more than what you
have assumed you are. We have been there. We have been in such a situation: when giving up or
settling with what is comfortable feels like the best choice. Luckily, the heart which is the dwelling
place for passion has told us otherwise.
It was in 2014 when our team was created. Our compass was this the dream of coming up with
books that can spread knowledge and education about programming. The goal was to reach as
many people across the world. For them to learn how to program and in the process, find
solutions, perform mathematical calculations, show graphics and images, process and store data
and much more. Our whole journey to make such dream come true has been very pivotal in our
individual lives. We believe that a dream shared becomes a reality.
We want you to be part of this journey, of this wonderful reality. We want to make learning
programming easy and fun for you. In addition, we want to open your eyes to the truth that
programming can be a start-off point for more beautiful things in your life.
Programming may have this usual stereotype of being too geeky and too stressful. We would like
to tell you that nowadays, we enjoy this lifestyle: surf-program-read-write-eat. How amazing is
that? If you enjoy this kind of life, we assure you that nothing is impossible and that like us, you
can also make programming a stepping stone to unlock your potential to solve problems,
maximize solutions, and enjoy the life that you truly deserve.
This book has found you because you are at the brink of everything fantastic!
Thanks for reading!
You can be interested in:
CSS: Learn CSS In A DAY!