0% found this document useful (0 votes)
25 views20 pages

Language Basics

The document discusses the basic lexical structure and syntax rules of PHP, including comments, data types, variables, operators, and expressions. Key points covered are: PHP is case insensitive for keywords but case sensitive for variables; statements require a semicolon while blocks do not; whitespace does not matter; and there are several types of operators like arithmetic, comparison, logical, and assignment operators.

Uploaded by

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

Language Basics

The document discusses the basic lexical structure and syntax rules of PHP, including comments, data types, variables, operators, and expressions. Key points covered are: PHP is case insensitive for keywords but case sensitive for variables; statements require a semicolon while blocks do not; whitespace does not matter; and there are several types of operators like arithmetic, comparison, logical, and assignment operators.

Uploaded by

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

Language Basics

Lexical structure
• The set of basic rules that governs how you
write programs in that language.
• It is the lowest-level syntax of the language
• Specifies such as
– what variable names look like,
– what characters are used for comments,
– and how program statements are separated from
each other.
• Case Sensitivity
– The names of user-defined classes and functions,
as well as built-in constructs and keywords such as
echo, while, class, etc., are case-insensitive
– Variables, on the other hand, are case-sensitive
• A statement
– A statement is a collection of PHP code that does something

• Use of semicolon in PHP


if ($needed) {
echo "We must have it!"; // semicolon required here
} // no semicolon required here

<?php
if ($a == $b) { echo "Rhyme? And Reason?"; }
echo "Hello, world" // no semicolon required before closing
tag
?>
• Whitespaces and line breaks
– Whitespace doesn’t matter in a PHP program

• Comments
– Shell style comments (# )
(Unix shell scripting Languages)
– C++ comments (// )
– C comments (/* */)
• Literals
– A literal is a data value that appears directly in a
program
• Identifiers
– In PHP, identifiers are used to name variables,
functions, constants, and classes.
• Keywords
– A word reserved by the language for its core
functionality—you cannot give a variable,
function, class, or constant the same name as a
keyword. Case insensitive in PHP.
Keywords
Data Types
PHP provides eight types of values, or data
types.
– Four are scalar (single-value) types: integers,
floating-point numbers, strings, and Booleans.
– Two are compound (collection) types: arrays and
objects.
– The remaining two are special types: resource and
NULL
Variables
• Variables in PHP are identifiers prefixed with a dollar sign
($). For example:
$name
$Age
$_debugging
$MAXIMUM_IMPACT

• A variable may hold a value of any type. There is no compile-


time or runtime type checking on variables. You can replace
a variable’s value with another of a different type
Variable Scope
• Local
• Global
• Static
• Function Parameters
Operators and Expressions
• Expression
– An expression is a bit of PHP that can be evaluated
to produce a value.
– The simplest expressions are literal values and
variables.
– A literal value evaluates to itself, while a variable
evaluates to the value stored in the variable.
– More complex expressions can be formed using
simple expressions and operators.
• Operator
– An operator takes some values (the operands) and
does something (for instance, adds them
together).
– Operators are written as punctuation symbols, for
instance, the ‘+’and ‘-’ familiar to us from math.
– Some operators modify their operands, while
most do not.
• Operators
– Unary
• Single operand e.g. ++x, +x etc
– Binary
• Two operands e.g. x+y, x<=y etc
– Ternary
• Used as a conditional statement
e.g.
if(x>y)?echo “X is larger”:echo “y is larger”;
Operators and Expressions

•Precedence
– The order in which operators in an expression are
evaluated depends on their relative precedence
•Associativity
– Associativity defines the order in which operators
with the same order of precedence are evaluated
Implicit casting
• Arithmetic Operator
– (+, -, *, /, %)
– Arithmetic negation (-3)
– Arithmetic assertion (+3)
• String concatenation (.)
• Auto increment and auto decrement
• Comparison operator
– >, >=, <, <=
• Following
• Logical Operators
– Logical AND (&&, and)
– Logical OR (||, or)
– Logical XOR (xor)
– Logical XOR (xor)
– Logical negation (!)
Casting Operators
• Although PHP is a weakly typed language, there are
occasions when it’s useful to consider a value as a
specific type.
• The casting operators allow you to force a value into
a particular type.
Assignment Operators

• Assignment (=)
• Assignment with operation (shorthand)
– Arithmetic (+=, -=, *=, /=, %=)
– Bitwise (~=, &=, |=)
– Concatenate-equal (.=)

You might also like