0% found this document useful (0 votes)
16 views33 pages

1.2. Introduction To PHP & Lexical Structure

The document provides an introduction to PHP and its lexical structure as part of a course module on PHP and JS Framework at SRI Krishna College of Engineering and Technology. It covers server-side programming, the purpose of PHP, and key components such as variables, constants, statements, and comments. Additionally, it explains the significance of case sensitivity and the use of whitespace and semicolons in PHP syntax.

Uploaded by

Karthikeyini S
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
16 views33 pages

1.2. Introduction To PHP & Lexical Structure

The document provides an introduction to PHP and its lexical structure as part of a course module on PHP and JS Framework at SRI Krishna College of Engineering and Technology. It covers server-side programming, the purpose of PHP, and key components such as variables, constants, statements, and comments. Additionally, it explains the significance of case sensitivity and the use of whitespace and semicolons in PHP syntax.

Uploaded by

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

SRI KRISHNA COLLEGE OF ENGINEERING AND TECHNOLOGY

Kuniamuthur, Coimbatore, Tamilnadu, India


An Autonomous Institution, Affiliated to Anna University,
Accredited by NAAC with “A” Grade & Accredited by NBA (CSE, ECE, IT, MECH ,EEE, CIVIL& MCT)

Course : 21CSI504 – PHP and JS Framework


Module : 1
Topic : Introduction to PHP & Lexical Structure
Faculty : Dr. S Karthikeyini
Department : M.Tech – Computer Science & Engineering

1
Topics Covered
▪ Server Side Programming
▪ What is PHP, Why PHP
▪ Lexical Structure
▪ Case Sensitivity
▪ Statements
▪ PHP comments
▪ PHP white space
▪ Semicolon
▪ PHP variables
▪ Constants
▪ PHP literal

2
Server-Side web programming
• Server-side pages are programs written using one of
many web programming languages/frameworks
– examples: PHP, Java/JSP, Ruby on Rails, ASP.NET, Python, Perl
• Web server:
– contains software that allows it to run server side programs
– sends back their output as responses to web requests
– we use PHP
What is PHP?

• PHP stands for "PHP Hypertext


Preprocessor"
• Server-side scripting language
• Used to make web pages dynamic:
– interface with other services: database,
e-mail, etc.
– authenticate users
– process form information
• PHP code can be embedded in XHTML code
PHP Editors

• Visual Studio Code


• PhpStorm
• NetBeans
• Eclipse PDT
• Notpad
• Notepad++
• Atom
• Bluefish
• …
Lifecycle of a PHP web request
Why PHP?

Free and
open
source

Co
m
mp le ibl pat
S i e
• PHP files can contain text, HTML,
CSS, JavaScript, and PHP code
What is • PHP code is executed on the server,
PHP files ? and the result is returned to the
browser as plain HTML
• PHP files have extension “.php”
Set Up PHP on Your Own PC

• install a web server


• install PHP
• install a database, such as MySQL
Lexical Structure
• The lexical structure of a programming language is the
set of basic rules that governs how you write programs in
that language.
• It is the lowest-level syntax of the language and specifies
such things as what variable names look like, what
characters are used for comments and how program
statements are separated from each other.
Cont.,
Statements
• A statement is a collection of PHP code that does
something.
• It can be as simple as a variable assignment or as
complicated as a loop with multiple exit points.
• Here is a small sample of PHP statements
• echo ”Hello, world”;
• A compound statement that uses curly braces to mark a
block of code, such as a conditional test or loop, does
not need a semicolon after a closing brace.
PHP comments

• Comments are used to clarify the source code


• All comments in PHP follow the # character.
• Compilers ignores the comment entries & do not execute
them.
• Two types of comments.
Cont.,
Cont.,
• Single line comments
• Used to comments single line
• Can be written in a separate line or along with code
in the same line.
• // comments.php
• Multiline comments
• Used to comment more than one line.
• Generally used comment entire block of code
statements
• /* comments.php
• Used to display Name */
PHP white space
• White space in PHP is used to separate tokens in PHP
source file. It is used to improve the readability of the
source code.
• Public $ is Running;
• White spaces are required in some places; for example
between the access specifier and the variable name.
• In other places, it is forbidden. It cannot be present in
variable identifiers
• $a = 1;
• $b = 2;
• $c = 3;
Semicolon
• A semicolon is used to mark the end of a statement in
PHP
• It is mandatory
• $a = 34;
• $b = $a * 34 – 34;
• echo $a;
• Here we have three different PHP statements
• The first is an assignment.
• It puts a value into the $a variable.
• The second one is an expression.
• The expression is evaluated and the output is given to
the $b variable.
• The third one is a command. It prints the $a variable
Cont.,
PHP variables
• A variable is an identifier, which holds a value.
• In programming we say that we assign a value to a variable.
• Technically speaking, a variable is a reference to a computer
memory, where the value is stored.
• In PHP languages, a variable can hold a string, a number, or
various objects like function or a class.
• Variables can be assigned different values over time.
• Variables in PHP consist of the $ character called a sigil and a
label.
• A label can be created from alphanumeric characters and an
underscore_character.
• A variable cannot begin with a number.
• The PHP interpreter can then distinguish between a number
and a variable more easily
• These were valid PHP variables.
• $Value
• $value2
• $company_name
• These were examples of invalid PHP variables.
• $12Val
• $exx$
• $first-name
• The variable are case sensitive
• This means that $Price, $price and $PRICE are three different
identifiers.
PHP Constants
• A constant is an identifier for a value which cannot change
during the execution of the script.
• By convention, constant identifiers are always uppercase.
• Constants are created with the define() function.
• define(“SIZE”,300)
• define(“EDGE”,100)
• Constants differ from variables
• We cannot assign a different value to an existing constant.
• The script will fail if we uncomment the line.
• #SIZE = 100;
• Constants do not use the dollar sign character.
• echo SIZE;
PHP literal
• A literal is any notation for representing a value within the
PHP source code.
• Technically, Literals are values that are used in code as they
are
• e.g. Numbers like 0,1,2,3 or string like ‘Welcome’.
• $age – 29;
• $nationality = “Hungarian”;
Hello World!
<?php
.
.
.

?>
PHP

<?php
echo "Hello, world!";
?>
PHP

Hello world!

output
PHP Case Sensitivity
• In PHP, NO keywords (e.g. If, else, while, echo), classes,
functions, and user-defined functions are case-sensitive.
• Note: However; all variable names are case-sensitive!
<?php
ECHO "Hello World!<br>";
echo "Hello World!<br>";
EcHo "Hello World!<br>";
?>
PHP
<?php
$color = "red";
echo "My car is " . $color . "<br>";
echo "My house is " . $COLOR . "<br>";
echo "My boat is " . $coLOR . "<br>";
?>
PHP
PHP syntax template
HTML content
<?php
PHP code
?>
HTML content
<?php
PHP code
?>
HTML content ... PHP

 Contents of a .php file between <?php and ?> are executed


as PHP code
 All other contents are output as pure HTML
 We can switch back and forth between HTML and PHP
"modes"
Console output: print/echo
 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.

print "text";
echo "text";

PHP
Console output: print/echo

print "Hello, World!\n";


print "Escape \"chars\" are the SAME as in Java!\n";
print "You can have
line breaks in a string.";
print 'A string can use "single-quotes". It\'s cool!';

PHP

Hello world! Escape "chars" are the SAME as in Java! You can have line
breaks in a string. A string can use "single-quotes". It's cool!

output
Comments
# single-line comment
// single-line comment
/*
multi-line comment
*/
PHP
 like Java, but # is also allowed
 a lot of PHP code uses # comments instead
of //
Summary
▪ Server Side Programming
▪ What is PHP, Why PHP
▪ Lexical Structure
▪ Case Sensitivity
▪ Statements
▪ PHP comments
▪ PHP white space
▪ Semicolon
▪ PHP variables
▪ Constants
▪ PHP literal
References
1. Steven Holzner, “PHP:The Complete
Reference”, McGraw Hill Education, 2017
2. https://www.w3schools.com/php/
3. https://www.tutorialspoint.com/php/index.htm
33

You might also like