Lesson 03
Lesson 03
2
PHP echo and print Statements
• In PHP there are two basic ways to get output:
• echo
• print.
• echo and print are more or less the same. They are both
used to output data to the screen.
• The differences are small:
• echo has no return value while print has a return value
of 1 so it can be used in expressions.
• echo can take multiple parameters (although such usage
is rare) while print can take one argument.
• echo is marginally faster than print.
3
PHP Script Example
<!DOCTYPE html>
<html>
<body> HTML Code
<?php
echo "Hello World!”;
print "Hello World”; PHP Code
?>
</body>
</html> HTML Code
Output -
Hello World!
Hello World
4
PHP Variables
• PHP supports a number of different variable types:
• integers
• floating point numbers
• strings
• arrays
• In many languages, it’s essential to specify the variable
type before using it: for example, a variable may need to be
specified as type integer or type array.
• PHP automatically determines variable type by the context
in which it is being used.
$variable_name
• Note that variable names in PHP are case sensitive.
5
PHP Variable Naming Conventions
• There are a few rules that you need to follow when
choosing a name for your PHP variables.
• PHP variables must start with a letter or underscore “_".
• PHP variables may only be comprised of alpha-numeric
characters and underscores. a-z, A-Z, 0-9, or _ .
• Variables with more than one word should be separated
with underscores. $my_variable
• Variables with more than one word can also be
distinguished with capitalization. $myVariable
$name, $INCOME, $_123
6
PHP Variables Example
<?php
// define variables
$name = 'Neo';
$rank = 'Anomaly';
$serialNumber = 1;
// print output
echo "Neo: I am <b>$name</b>, the <b>$rank</b>. You can
call me by my serial number, <b>$serialNumber</b>.”;
php?>
8
PHP Variable Type Casting
• The diagram below shows PHP implementing the above
example.
• PHP also allows you to cast the data type. This is known as
explicit casting. The code below demonstrates explicit type
casting.
$a = 1;
$b = 1.5;
$c = $a + $b;
$c = $a + (int) $b;
echo $c;
9
PHP Variable Type Casting
• The var_dump function is used to determine the data type.
The code below demonstrates how to use the var_dump
function.
$a = 1;
var_dump($a);
$b = 1.5;
var_dump($b);
$c = "I Love PHP";
var_dump($c);
$d = true;
var_dump($d);
Output:
int(1) float(1.5) string(10) "I Love PHP" bool(true)
10
PHP Data Types
• Alphanumeric characters are classified as strings
• Whole numbers are classified as integers
• Numbers with decimal points are classified as floating
points.
• True or false values are classified as Boolean.
11
PHP Data Types
• PHP implicitly supports the following data types,
• Integer – whole numbers e.g. -3, 0, 69. $x = 1;
The maximum value of an integer is platform-dependent. The
constant PHP_INT_MAX is used to determine the maximum
value.
echo PHP_INT_MAX;
• Floating point number – decimal numbers e.g. 3.14. they are
also known as double or real numbers. The maximum value
of a float is platform-dependent. Floating point numbers are
larger than integers.
• Character string – e.g. Hello World $y = "foo";
• Boolean – e.g. True or false. $z = True;
• Null - $my_var = NULL;
12
PHP Constant
• A constant is a variable whose value cannot be changed at
runtime.
• Suppose we are developing a program that uses the value
of PI 3.14, we can use a constant to store its value.
define('PI',3.14); //creates a constant
with a value of 3.14
• Once you define PI as 3.14 , writing a code like below will
generate an error
PI = 4; //PI has been defined as a
constant therefore assigning a value is
not permissible.
13
Questions?
Thank You
14