Language Basics
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
<?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
•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 (.=)