0% found this document useful (0 votes)
61 views

Course: PHP and Mysql: Lecture Three Presented By: Abdalle Muhumed Geele

This document provides an overview of Chapter 2 from a PHP and MySQL lecture. The chapter objectives are to code a basic PHP application using variables, echo statements, and expressions. It covers topics like embedding PHP in HTML, PHP comments and statements, data types, variables, constants, and the echo statement. Examples are provided for declaring variables of different data types, assigning values, and using echo to output data. The document appears to be slides from a lecture on basic PHP coding concepts.

Uploaded by

Fadxiya Muxumed
Copyright
© © All Rights Reserved
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
61 views

Course: PHP and Mysql: Lecture Three Presented By: Abdalle Muhumed Geele

This document provides an overview of Chapter 2 from a PHP and MySQL lecture. The chapter objectives are to code a basic PHP application using variables, echo statements, and expressions. It covers topics like embedding PHP in HTML, PHP comments and statements, data types, variables, constants, and the echo statement. Examples are provided for declaring variables of different data types, assigning values, and using echo to output data. The document appears to be slides from a lecture on basic PHP coding concepts.

Uploaded by

Fadxiya Muxumed
Copyright
© © All Rights Reserved
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 32

Course: PHP and MySQL

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

// C++ and Java-style 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

PHP supports two numeric data types:


•An integer is a positive or negative number with no decimal places
(-250, 2, 100, 10,000)
•A floating-point number is a number that contains decimal places

Chapter 1 Slide 11
Boolean Values

•A Boolean value is a value of true or false


•It decides which part of a program should execute and which part
should compare data
•In PHP programming, you can only use true or false
•In other programming languages, you can use integers such as
1 = true, 0 = false

Chapter 1 Slide 12
Integer values (whole numbers)
15 // an integer
-21 // a negative integer

Double values (numbers with decimal positions)


21.5 // a floating-point value
-124.82 // a negative floating-point value

The two Boolean values


true // equivalent to true, yes, or on
false // equivalent to false, no, or off

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

Specifying and creating a variable name is called declaring the


variable
Assigning a first value to a variable is called initializing the
variable
In PHP, you must declare and initialize a variable in the same
statement:
$variable_name = value;

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

course of program execution


 Constant names do not begin with a dollar sign

 Constant names use all uppercase letters

 Use the define() function to create a constant

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,

number, or Boolean value

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;

How to use an echo statement within HTML


<p>Name: <?php echo $name; ?></p>

How to use an echo statement to output HTML


tags and data
<?php
echo '<p>Name: ' . $name . '</p>';
?>

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';

Assign NULL values and empty strings


$address2 = ''; // an empty string
$address2 = null; // a NULL value

Use double quotes for variable substitution (interpolation)


$name = "Name: $first_name"; // Name: Bob
$name = "$first_name $last_name"; // Bob Roberts

Mix single and double quotes for special purposes


$last_name = "O'Brien"; // O'Brien
$line = 'She said, "Hi."'; // She said, "Hi."

Chapter 2 Slide 29
Chapter 2 Slide 30
Any Question??
Thanks

You might also like