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

Lesson 03

The document provides an overview of PHP syntax and variables. It explains that PHP code is placed between <?php ?> tags, and files typically have a .php extension. It also covers PHP echo and print statements, variable naming conventions, implicit and explicit type casting, and defining constants.

Uploaded by

DK White Lion
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views

Lesson 03

The document provides an overview of PHP syntax and variables. It explains that PHP code is placed between <?php ?> tags, and files typically have a .php extension. It also covers PHP echo and print statements, variable naming conventions, implicit and explicit type casting, and defining constants.

Uploaded by

DK White Lion
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 13

PHP Syntax

• A PHP script can be placed anywhere in the document.


• A PHP script starts with <?php and ends with ?>:
<?php
// PHP code goes here
?>

• The default file extension for PHP files is “.php".


• A PHP file normally contains HTML tags, and some PHP
scripting code.

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

<h1>My first PHP page</h1>

<?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?>

Here, the variables $name, $rank and $serialNumber are first


defined with string and numeric values, and then substituted in
the echo() function call.
7
PHP Variable Type Casting
• Type casting is converting a variable or value into a desired
data type.
• This is very useful when performing arithmetic computations
that require variables to be of the same data type.
• Type casting in PHP is done by the interpreter.
• In other languages such as C#, you have to cast the
variables. The code below shows type casting in C#.

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.

• PHP is a loosely typed language; it does not have explicit


defined data types.
• PHP determines the data types by analyzing the attributes
of data supplied.

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

You might also like