Course: PHP and Mysql: Lecture Three Presented By: Abdalle Muhumed Geele
Course: PHP and Mysql: Lecture Three Presented By: Abdalle Muhumed Geele
Lecture Three
Presented by:
Abdalle Muhumed Geele.
Chapter 2
How to code
a PHP application
Chapter 2 Slide 2
Objectives
Applied
Given the specifications for a PHP application that
requires only the skills and language elements presented in
this chapter, code, test, and debug the application.
That includes these skills:
Creating variables with valid names and assigning values
to them.
Using echo statements to display data on a page
Coding string and numeric expressions
Chapter 2 Slide 3
Objectives (continued)
Knowledge
Explain how PHP is embedded within an HTML document.
Distinguish between PHP statements and comments.
Describe these PHP data types: integer, double, Boolean, and string.
List the rules for creating a PHP variable name.
Describe the code for declaring a variable and assigning a value to it.
Describe the use of the echo statement.
Chapter 2 Slide 4
Comments in PHP
Comments are nonprinting lines placed in code such as:
•The name of the script
•Your name and the date you created the program
•Notes to yourself
•Instructions to future programmers who might need to modify your
work
•Line comments: hide a single line of code
•Add // or # before the text
•Block comments: hide multiple lines of code
•Add /* to the first line of code
•And */ after the last character in the code
Chapter 1 Slide 5
Comments in PHP
Standard C, C++, and shell comment symbols
Two types of comment during PHP
1)One line comment
2)Two or more line comment
# Shell-style comments
/* C-style comments
These can span multiple lines */
Chapter 2 Slide 6
PHP code: comments and statements
<?php
/*
This program calculates the discount for a
price that's entered by the user
*/
?>
Another way to code single-line comments
// get the data from the form
Chapter 2 Slide 7
Syntax rules
PHP statements end with a semicolon.
PHP ignores extra whitespace in statements.
Chapter 2 Slide 8
Data types
A data type is the specific category of information that a variable
contains
Data types that can be assigned only a single value are called
primitive types
Chapter 2 Slide 9
Data types cont…..
The PHP language supports:
•A resource data type – a special variable that holds a reference
to an external resource such as a database or XML file
•Reference or composite data types, which contain multiple
values or complex types of information
•Two reference data types: arrays and objects
Chapter 1 Slide 10
Numeric Data Types
Chapter 1 Slide 11
Boolean Values
Chapter 1 Slide 12
Integer values (whole numbers)
15 // an integer
-21 // a negative integer
String values
'Ray Harris' // a string with single quotes
"Ray Harris" // a string with double quotes
'' // an empty string
null // a NULL value
Chapter 2 Slide 13
Variables in PHP
The values stored in computer memory are called variables
The name you assign to a variable is called an identifier and it:
•Must begin with a dollar sign ($)
•Cannot begin with an underscore (_) or a number
•Cannot include spaces
•Is case sensitive
Chapter 1 Slide 14
Rules for creating variable names
PHP variables must begin with a dollar sign "$"
Variable names are case-sensitive.
Variable names can contain letters, numbers, and underscores.
Variable names can’t contain special characters.
Variable names can’t begin with a digit or two underscores.
Variable names can’t use names that are reserved by PHP such as
the variable named $this that’s reserved for use with objects.
Chapter 2 Slide 15
Declaring and Initializing Variables
Chapter 1 Slide 16
How to declare variables and constants
Example of positive integer number
<?php
$x=5;
Echo $x;
?>
Example of negative integer number
<?php
$x=-5;
Echo $x;
?>
Chapter 2 Slide 17
Example of positive double number
<?php
$x=5.7;
Echo $x;
?>
Example of negative double number
<?php
$x=-5.7;
Echo $x;
?>
Chapter 2 Slide 18
Example of Boolean
<?php
$x=true;
Echo $x;
?>
Example of Boolean
<?php
$x=false;
Echo $x;
?>
Chapter 2 Slide 19
Example of String
<?php
$name="Mohamed";
Echo $name;
?>
Example of String
<?php
$NAME="MOHAMED";
$name="Ahmed";
Echo $name;
?>
Chapter 2 Slide 20
Example of String (single or double)
<?php
$x='Ali';
echo " x is equal to $x </br>"; //Value is output
echo ' x is equal to $x'; // Variable is output
?>
Chapter 2 Slide 21
Example of Array
<?php
$x[0]="Mohamed";
$x[1]="Ahmed";
$x[2]="Ali";
$x[3]="Ibrahim";
echo $x[2];
?>
Chapter 2 Slide 22
Chapter 2 Slide 23
Embedded file that includes HTML and PHP code
<html>
<head>
<title> Integration </title>
</head>
<body>
<?php
$x=5;
$y=6;
$z=$x+$y;
?>
</body>
</html>
<?php
echo $z;
?>
Chapter 2 Slide 24
Constant
A constant contains information that does not change during the
define("CONSTANT_NAME", value);
define("VOTING_AGE",18);
define("VOTING_AGE",18,TRUE);
The value you pass to the define() function can be a text string,
Chapter 1 Slide 25
How to declare a constant
define('MAX', 100); // an integer constant
define('PI', 3.14); // a double constant
define('MALE', 'm'); // a string constant
Examples:
<?php
define('MAX', 100);
define('PI', 3.14);
define('MALE', 'm');
echo MAX; echo"</br>";
echo PI; echo"</br>";
echo MALE;
?>
Chapter 2 Slide 26
Chapter 2 Slide 27
The syntax for the echo statement
echo string_expression;
Chapter 2 Slide 28
How to work with data How assign string expressions
Use single quotes to improve PHP efficiency
$first_name = 'Bob';
$last_name = 'Roberts';
Chapter 2 Slide 29
Chapter 2 Slide 30
Any Question??
Thanks