Week 3
Week 3
Web Programming
Week3:- Basics of PHP
Variables
PHP variables
• You must place a $ in front of all variables.
• PHP automatically determines variable type by the context in which it is being used.
• Example
• $variable_name;
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
PHP Data types
• PHP is a loosely typed language; it does not have explicitly defined
data types.
• PHP determines the data types by analyzing the attributes of the data
supplied.
• 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.
Variable assignment
• The syntax to assign a value to a variable is always
variable = value.
• Types of operators
• Arithmetic operators
• Assignment operators
• Comparison operators
• Logical operators
Arithmetic Operators
Operator Description Example
+ Additon $a+1
- Substraction $a-6
* Multiplicaton $a*40
/ Division $a/5
= $a=10 $a=10
+= $a+=5 $a =$a+5
-= $a-=2 $a=$a-2
*= $a*=10 $a=$a*10
/= $a/=5 $a=$a/5
%= $a%=3 $a=$a%3
== Is equal to $a == 5