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

Week 3

This document discusses PHP variables, data types, and operators. It covers PHP variable naming conventions and types. It also describes different types of operators used in PHP like arithmetic, assignment, comparison, and logical operators.

Uploaded by

santhirans770
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views

Week 3

This document discusses PHP variables, data types, and operators. It covers PHP variable naming conventions and types. It also describes different types of operators used in PHP like arithmetic, assignment, comparison, and logical operators.

Uploaded by

santhirans770
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 15

HNDIT3022

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.

• PHP supports a number of different variable types:


• integers , floating point numbers, strings and arrays

• Variable names in PHP are case-sensitive.

• 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.

• Or, to reassign the value to another variable, it is other


variable = variable
Constants
• 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);

• define is a predefined function that is used to create constants in PHP.


Operators
Operators
• Operators are the mathematical, string, comparison, and logical
commands such as plus, minus, multiply, and divide.

• 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

% Modulus(Division Reminder) $a%2

++ Increment $a++ or ++$a

-- Decrement $a– or --$a


Assignment Operators
Operator Example Equivalent to

= $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

.= $a.=“A” $a=$a . “A”


Comparison operators
Operator Description Example

== Is equal to $a == 5

!= Is not equal to $a!=5

> Is greater than $a>10

< Is less than $a<10

>= Is greater than or equal to $a>=10

<= Is less than or equal to $a<=10


Logical Operators

Operator Description Example


&& And $a==3 && $b==2
and Low-precedence and $a==3 and $b==2
|| Or $a==3 || $b==2
or Low-precedence or $a==3 or $b==2
! Not !($a==$b)
xor Exclusive or $a xor $b
Questions ?
Thank You !

You might also like