Вы находитесь на странице: 1из 40

Web Development 1

PHP Programming Fundamentals

PHP PROGRAMMING FUNDAMENTALS

At the end of the module the student is expected to:

1. Understand and apply the different types of operators


a. Arithmetic
b. Comparison
c. Increment and decrement operators
d. Logical operators
e. String operators
2. Understand how to use special characters for string delimiters
3. Use HTML table for displaying data.
4. Application of conditional statements
a. If statement
b. If else statement
5. Application of AND, OR, NOT clause
6. Applying field validation PHP programs
7. Understand and apply div in HTML and PHP

INTRODUCTION

PHP Operators

Operators are used to perform operations on variables and values.

PHP divides the operators in the following groups:

 Arithmetic operators
 Assignment operators
 Comparison operators
 Increment/Decrement operators
 Logical operators
 String operators

PHP Arithmetic Operators

The PHP arithmetic operators are used with numeric values to perform common
arithmetical operations, such as addition, subtraction, multiplication etc.

Course Module 1
Web Development 2
PHP Programming Fundamentals

Listed below are application / sample of arithmetic operators.

Sample

<html> <html> <html>


<body> <body> <body>

<?php <?php <?php


//addition //subtraction //multiplication
$x = 10; $x = 10; $x = 10;
$y = 6; $y = 6; $y = 6;

echo $x + $y; echo $x - $y; echo $x * $y;


?> ?> ?>

</body> </body> </body>


</html> </html> </html>
Answer: 16 Answer: 4 Answer: 60

<html> <html>
<body> <body>

<?php <?php
//division //modulus
$x = 10; $x = 10;
$y = 6; $y = 6;

echo $x / $y; echo $x % $y;


?> ?>

</body> </body>
</html> </html>
Answer: 1.6666666666667 Answer: 4
PHP Assignment Operators

Course Module 2
Web Development 3
PHP Programming Fundamentals

The PHP assignment operators are used with numeric values to write a value to a
variable.

The basic assignment operator in PHP is "=". It means that the left operand gets set
to the value of the assignment expression on the right.

Sample

<html> <html> <html>


<body> <body> <body>

<?php <?php <?php


$x = 10; $x = 20; $x = 50;
echo $x; $x += 100; $x -= 30;
?>
echo $x; echo $x;
</body> ?> ?>
</html>
</body> </body>
</html> </html>

Answer: 10 Answer: 120 Answer: 20

<html> <html> <html>


<body> <body> <body>

<?php <?php <?php


$x = 10; $x = 10; $x = 15;
$y = 6; $x /= 5; $x %= 4;

echo $x * $y; echo $x; echo $x;


?> ?> ?>

</body> </body> </body>


</html> </html> </html>

Answer: 60 Answer: 2 Answer: 3

Course Module 3
Web Development 4
PHP Programming Fundamentals

PHP Comparison Operators

The PHP comparison operators are used to compare two values (number or string):

Sample

Course Module 4
Web Development 5
PHP Programming Fundamentals

<html> <html> <html>


<body> <body> <body>

<?php <?php <?php


$x = 100; $x = 100; $x = 100;
$y = "100"; $y = "100"; $y = "100";

var_dump($x == $y); // returns true var_dump($x === $y); // returns var_dump($x != $y); // returns false
because values are equal false because types are not equal because values are equal
?> ?> ?>
</body> </body> </body>
</html> </html> </html>

Answer: bool(true) Answer: bool(false) Answer: bool(false)

<html> <html> <html>


<body> <body> <body>

<?php <?php <?php


$x = 100; $x = 100; $x = 100;
$y = "100"; $y = "100"; $y = 50;

var_dump($x <> $y); // returns false var_dump($x !== $y); // returns true var_dump($x > $y); // returns true
because values are equal because types are not equal because $x is greater than $y
?> ?> ?>
</body> </body> </body>
</html> </html> </html>

Answer: bool(false) Answer: bool(true) Answer: bool(true)

<html> <html> <html>


<body> <body> <body>

<?php <?php <?php


$x = 10; $x = 50; $x = 50;
$y = 50; $y = 50; $y = 50;

var_dump($x < $y); // returns true var_dump($x >= $y); // returns true var_dump($x <= $y); // returns true
because $x is less than $y because $x is greater than or equal to because $x is less than or equal to $y
?> $y ?>
</body> ?> </body>
</html> </body> </html>
</html>

Answer: bool(true) Answer: bool(true) Answer: bool(true)

PHP Increment / Decrement Operators

The PHP increment operators are used to increment a variable's value.The PHP
decrement operators are used to decrement a variable's value.

Course Module 5
Web Development 6
PHP Programming Fundamentals

Sample
<html> <html>
<body> <body>

<?php <?php
$x = 10; $x = 10;
echo ++$x; echo $x++;
?> ?>

</body> </body>
</html> </html>

Answer: 11 Answer: 10
<html> <html>
<body> <body>

<?php <?php
$x = 10; $x = 10;
echo --$x; echo $x--;
?> ?>

</body> </body>
</html> </html>

Answer: 9 Answer: 10

Course Module 6
Web Development 7
PHP Programming Fundamentals

PHP Logical Operators

The PHP logical operators are used to combine conditional statements.

Sample

<html> <html> <html>


<body> <body> <body>
<?php <?php <?php
$x = 100; $x = 100; $x = 100;
$y = 50; $y = 50; $y = 50;
//and //or //xor
if ($x == 100 and $y == 50) { if ($x == 100 or $y == 80) { if ($x == 100 xor $y == 80) {
echo "Hello world!"; echo "Hello world!"; echo "Hello world!";
} } }
?> ?> ?>
</body> </body> </body>
</html> </html> </html>

<html> <html> <html>


<body> <body> <body>

<?php <?php <?php


//&& || $x = 100;
$x = 100; $x = 100; //!==
$y = 50; $y = 50; if ($x !== 90) {
echo "Hello world!";
if ($x == 100 && $y == 50) { if ($x == 100 || $y == 80) { }
echo "Hello world!"; echo "Hello world!"; ?>
} }

Course Module 7
Web Development 8
PHP Programming Fundamentals

?> ?> </body>


</body> </body> </html>
</html> </html>

PHP String Operators

PHP has two operators that are specially designed for strings.

<html> <html>
<body> <body>

<?php <?php
$txt1 = "Hello"; $txt1 = "Hello";
$txt2 = " world!"; $txt2 = " world!";
echo $txt1 . $txt2; $txt1 .= $txt2;
?> echo $txt1;
?>
</body> </body>
</html> </html>

Course Module 8
Web Development 9
PHP Programming Fundamentals

String Delimiters

In this section, if in case you need to display special characters such as ( ‘ ), you
cannot just include it into a echo statement you need to use ( \ ) to display it.

The HTML code shows the


application of string delimiters,
as you can see in the output “Hey
Johann!” I said Don’t do that”.
We cannot just include the use of
(‘) characters to php, if literarily
you do that it will cause errors.

For the code refer to:


18string_delimiters.html

Here is the result of the data


submission.

The output shows the


application of displaying the (‘)
character to the text,

For the code refer to:


18string_delimiters.php

Course Module 9
Web Development 10
PHP Programming Fundamentals

The HTML code shows that it


allows the user to input name
using a textbox after the input
then the user needs to press the
button to submit the data to the
php script.

Filename:
18string_delimiters.html

Upon submission of data from


the HTML form, the

Filename:
18string_delimiters.php

Arithmetic Operations

Course Module 10
Web Development 11
PHP Programming Fundamentals

The HTML code shows the


application of arithmetic
operators.

Here the user is asked to input


apples and oranges.

Next is to tick the submit button


to display the result

For the code refer to:


19_simple_math.html

Here is the result of the data


submission.

Upon submission, the php script


takes into action, the script will
perform basic arithmetic
operators

For the code refer to:


19_simple_math.php

Course Module 11
Web Development 12
PHP Programming Fundamentals

The HTML code shows that it


allows the user to input total
number of apples and oranges.

After the input, comes the


processing operation.

Filename:
19_simple_math.html

Upon submission of data from


the HTML form. The script
performs basic arithmetic
operators: addition, subtraction,
multiplication, division and
modulo.

Filename:
19_simple_math.php

Course Module 12
Web Development 13
PHP Programming Fundamentals

Using HTML Table

The HTML tables allow web authors to arrange data like text, images, links, other tables,
etc. into rows and columns of cells. The HTML tables are created using the <table> tag in
which the <tr> tag is used to create table rows and <td> tag is used to create data cells.

The output of this is to display


the result applying the use of
tables. Here the display is
display into two tables making it
more organize. Same as the
previous activity, it ask the user
to input total number of apples
and oranges.

For the code refer to:


20html_table.html

Here is the result of the data


submission.

The result groups the data into


two columns that makes the
output more easier to read.

For the code refer to:


20html_table.php

Course Module 13
Web Development 14
PHP Programming Fundamentals

The HTML code shows the


application of table and dividing
the output into two columns.

With this the output is more


organized.

Filename:
20html_table.html

Upon submission of data from


the HTML form.

The initial part of the script


computes the total number of
fruits using basic operators.

The script also display the result


into table.

Filename:
20html_table.php

Course Module 14
Web Development 15
PHP Programming Fundamentals

Using If Statement

The HTML form allow the users


to input firstname and birth
year. After the input the data is
passed to the php script for some
process.

For the code refer to:


21if_statement.html

Here is the result of the data


submission.

The output shows the


application of displaying name of
the user and age. If the input age
if >50 the message better start
planning for retirement, see php
code for the condition.

For the code refer to:


21if_statement.php

Course Module 15
Web Development 16
PHP Programming Fundamentals

The HTML code shows that it


allows the user to input
firstname and birth year. The
data are stored in variables and
is passed to the php code.

Filename:
21if_statement.html

Course Module 16
Web Development 17
PHP Programming Fundamentals

Upon submission of data from


the HTML form, the php code
gets the firstname, birthyearand
current year using the date()
function. The php script also
computes for the age by
subtracting the current year and
birthyear.

A condition is tested that if the


age is > 50, if the statement is
true the statement “Better start
planning will display” otherwise
just show the age and name of
the user.

Filename:
21if_statement.php

Course Module 17
Web Development 18
PHP Programming Fundamentals

Using Else clause

This is same as the previous


example. The html code asked
the use to input the firstname
and birthyear, then its passed to
the php script for processing

For the code refer to:


22else_clause.html

Upon submission of data from


the html form, the result is to
display the name and the year of
birth.

This time it uses the else clause


where the condition is

If age>50 – displays the message


better plan for retirement
otherwise dispay time is on your
side.

For the code refer to:


22else_clause.php

Course Module 18
Web Development 19
PHP Programming Fundamentals

The HTML code shows that it


allows the user to input
firstname and birth year. The
data are stored in variables and
is passed to the php code.

Filename:
22else_clause.html

Upon submission of data from


the HTML form, the php code
gets the firstname, birthyear and
current year using the date()
function. The php script also
computes for the age by
subtracting the current year and
birthyear.

A condition is tested that if the


age is > 50, if the statement is
true the statement “Better start
planning will display” otherwise
displays the message time is on
your side.

Filename:
22else_clause.php

Using nested if
Course Module 19
Web Development 20
PHP Programming Fundamentals

The HTML code this time shows


three textboxes; it asks the user
to input 3items, firstname,
numbers of hours sleep at night
and birth year, the data upon
pressing submit information is
process in the php script.

For the code refer to:


23nested_if.html

Upon submission of the HTML


form, it displays the result by
showing the firstname, number
of hours sleep and using
condition determine the age with
corresponding suggestions.

For the code refer to:


23nested_if.php

Course Module 20
Web Development 21
PHP Programming Fundamentals

The HTML codes shows the


needed attribute such as
firstname, number of hours sleep
at night and birth year. Upon
submission the data is passed to
a php script.

Filename:
23nested_if.html

Upon submission of data from


the HTML form, it passed to
several variables. It computes
for age and years of sleep.

A conditional statement is
determine with the following
consideration, if age > 50 prints
better start for retirement, if
years_sleep is greater than 15
prints you sleep to much
otherwise yes I mean it,

This is example of a nested if


statement.

Filename:
23nested_if.php

Code formatting rules

Course Module 21
Web Development 22
PHP Programming Fundamentals

The HTML code illustrate the


application of code formatting.
The code is the same as the
previous examples, the focus of
this is the way that the codes are
written

For the code refer to:


24code_formatting_rules.html

Course Module 22
Web Development 23
PHP Programming Fundamentals

Here is the result of the data


submission.

It display the name and the


number of years spent in
sleeping.

For the code refer to:


24code_formatting_rules.php

The HTML codes shows the


needed attribute such as
firstname, number of hours sleep
at night and birth year. Upon
submission the data is passed to
a php script.

Filename:
24code_formatting_rules.html

Course Module 23
Web Development 24
PHP Programming Fundamentals

Proper alignment of codes is not


a requirement in php coding.
Even if the codes are written
same with the image, it will still
display the result. It would be a
great advance to have proper
coding readability.

Filename:
24code_formatting_rules.php

Comparison Operators

The HTML code shows the


application of comparison
operators. The code accepts
firstname, year of birth then
submits the information and
display to the appropriate php
script.

For the code refer to:


25comparison_operators.html

Course Module 24
Web Development 25
PHP Programming Fundamentals

Here is the result of the data


upon submission.

The script shows the firstname


and the comment that it displays
the text the name jeiven comes
before the letter M in the
alphabet.

See the code for tracing.

For the code refer to:


25comparison_operators.php

The HTML code displays the


firstname, birth year and
submission button.

Filename:
25comparison_operators.html

Course Module 25
Web Development 26
PHP Programming Fundamentals

The script illustrate the


application of if statement. If the
$fistname == ‘Joan’ which is a
comparison statement it
compares the input text to the
text ‘Joan’ if it matches then it
moves to the next part of the
script that is to test the
birthyear.

Filename:
25comparison_operators.php

Course Module 26
Web Development 27
PHP Programming Fundamentals

Using AND

The HTML code shows the


application of AND operator.

It requires the users to input


three items, firstname, number
of hours sleep and birthyear.

For the code refer to:


26using_and.html

Here is the result of the data


submission.

It displays the firstname and


total numbers years sleep.

See the php sript for the


application of the and operator.

For the code refer to:


26using_and.php

Course Module 27
Web Development 28
PHP Programming Fundamentals

The HTML code displays the


firstname, birth year, number of
hours sleep and submission
button.

Filename:
26using_and.html

The php script collects data from


the html form, it computes for
the age, years slept and prints
the result.

Using the && operator it


determine if the age is >50 and
years is > 15, it its true then it
displays the text better start
planning and you sleep too much
otherwise, time is on your side.

Filename:
26using_and.html

Using OR

Course Module 28
Web Development 29
PHP Programming Fundamentals

The HTML code shows the


application of OR operator.

It requires the users to input


three items, firstname, number
of hours sleep and birthyear.

For the code refer to:


27using_OR.html

Here is the result of the data


submission.

It displays the firstname and


total numbers years sleep.

See the php sript for the


application of the OR operator

For the code refer to:


27using_OR.php

Course Module 29
Web Development 30
PHP Programming Fundamentals

The HTML code displays the


firstname, birth year, number of
hours sleep and submission
button.

Filename:
27using_OR.html

The php script collects data from


the html form, it computes for
the age, years slept and prints
the result.

Using the || operator it


determine if the age is >50 and
years is > 15, it its true then it
displays the text better start
planning and you sleep too much
otherwise, time is on your side.

Filename:
27using_OR.html

USING NOT

Course Module 30
Web Development 31
PHP Programming Fundamentals

The HTML code shows the


application of the NOT operator

It ask the user to input its


firstname and select form a list
of cities.

Then after completing the details


submits the data via the submit
button.

For the code refer to:


28using_not.html

The output is to display the


name given by the user and
display the city.

The code shows the application


of the NOT operator.

For the code refer to:


28using_not.php

Course Module 31
Web Development 32
PHP Programming Fundamentals

The HTML codes shows the


inputs that is needed to be
supplied by the user. First name
and select from the drop down
list box.

Filename:
28using_not.html

Upon submission of data from


the HTML form, the php script
kicks in.

The variables collected where


firstname and city.

The if else statement determine


if the input city is !Toronte then
the message you don’t live in the
best city, otherwise Toronto
rocks.

Filename:
28using_not.php

Field Validation

Course Module 32
Web Development 33
PHP Programming Fundamentals

The HTML code shows the


application of Field Validations.
It asked the user to input three
fields, firstname, number of
hours sleep at night and birth
year.

The data is passed to the php


script for proessing.

For the code refer to:


29field_validations.html

Here is the output of the HTML


form.

It displays the name and the


number of year sleeping.

If the user forgot to input the


firstname the validation scripts
comes into play.

For complete code see the php


script.

For the code refer to:


29field_validations.php

Course Module 33
Web Development 34
PHP Programming Fundamentals

The HTML code shows the fields


needed by the user it require the
user to input firstname, number
of years sleep and birth year.

Filename:
29field_validations.html

Upon submission of data from


the HTML form, the php scripts
checks the following areas, if the
input values are empty, it
informs the user that they much
enter values for the firstname
and hours of sleep field.

It script also check if the input


field for the hours sleep is
numeric otherwise inform the
user to input numeric values.

Filename:
29field_validations.php

Course Module 34
Web Development 35
PHP Programming Fundamentals

Course Module 35
Web Development 36
PHP Programming Fundamentals

Using DIV’s

The <div> tag defines a division or a section in an HTML document. The <div> tag is used
to group block-elements to format them with CSS.

The HTML code shows the


application of string delimiters,
as you can see in the output “Hey
Johann!” I said Don’t do that”.
We cannot just include the use of
(‘) characters to php, if literarily
you do that it will cause errors.

For the code refer to:


30using_divs.html

The HTML codes includes inline


CSS codes to produce two divs,
fruits and yourage

DIVs and Forms

Course Module 36
Web Development 37
PHP Programming Fundamentals

The HTML form shows the


application of DIVS, there are
two section, first is the total
number of apples and pears. The
other is the computation of age.

For the code refer to:


31using_divs_and_forms.html

There are two possible output


depending on the user selection.

This section shows the fruits


output.

For the code refer to:


31fruits.php

Course Module 37
Web Development 38
PHP Programming Fundamentals

This section shows the age php


script.

For the code refer to:


31_your_age.php

The HTML code shows the


application of div in action.
There are two action being
executed by this code, it calls for
the fruits and age scripts upon
submission of the user.

Filename:
31using_divs_and_forms.html

Course Module 38
Web Development 39
PHP Programming Fundamentals

Upon submission of data from


the HTML form, the fruits show
the computation of the total
number of apples and pears.

Filename:
31fruits.php

Upon submission of data from


the HTML form. This script
computes for the total years, the
script also shows the
implementation of if statement.

Filename:
31_your_age.php

Course Module 39
Web Development 40
PHP Programming Fundamentals

References
Murach, J. (2014) Murach’s PHP and MYSQL (2nd Edition)

Yank, K, PHP and MYSQL: Novice to Ninja

WEBSITE

http://php.net/

http://www.w3schools.com/php/

https://www.tutorialspoint.com/php/

Course Module 40

Вам также может понравиться