You are on page 1of 19

PHP MYSQL

Lecture 03: Variables


 The fundamental units of every
programming language are
variables.
 The variable is a structure in
programming that may be used for
storing both quantitative and text-
Department of Computer
based information. Science
 The contents of a variable can be
Semester 6
changed during program execution,
and variables can be compared and Lecturer Yasir A. Mohamed
modified using operators
Cont..
 Integers, floating point numbers, strings, arrays, and
NULLs are just a few of the variable types that PHP offers.
 The language may automatically detect the variable type
based on the way in which it is being used.
 Declaring variables is not required. Every variable has a
name, which must start with a letter or underscore
character and is followed by the dollar ($) sign.
Cont..
 optionally followed by more letters, numbers, and
underscores.

 For example, $oneday, $one_day, and $ONEDAY are all


valid PHP variable names, while $123 and $48hrs are
invalid variable names
Cont..
 Variable names in PHP are case-sensitive; For example:
$count is different from $Count or $COUNT.
 Saving form input in variable PHP can be used for creating
dynamic websites.
 In a dynamic website, user can interact with website by
providing text or by selecting options.
 Form is one of the quickest and easiest ways to add
interactivity to our website.
Rules for PHP variables:
 A variable starts with the $ sign, followed by the name of
the variable
 A variable name must start with a letter or the underscore
character
 A variable name cannot start with a number
 A variable name can only contain alpha-numeric
characters and underscores (A-z, 0-9, and _ )
 Variable names are case-sensitive ($age and $AGE are two
different variables)
Example 01 Variables
 PHP echo statement is often used to output data to the
screen.
 The following example will show how to output text and
a variable:
 The variable value will be the name Semesterka Lixaad
and the variable name will be $text
 Lets practically exercise this below example
Example
Browser Result
Example 02 Variables
 The following example will output the sum of two
variables:
 Now we are trying to assign two variable $num1 and
$num2 and values 20 and 50
Example
two
Browser
Result
Example 03 Variables
 In PHP, variable names are case sensitive.
 So variable name "color" is different from Color, COLOR,
COLor etc
Example
Three
Browser
Result
PHP echo and print
Statements
 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
 echo can take multiple parameters
 while print can take one argument.
 echo is a little bit faster than print.
CLASS PRACTICE
1. Write a program to print “Hello PHP” using php variable?
 You can not use text directly in echo but can use
variable.
2. Write a program to print “Welcome to the PHP World”
using some part of the text in variable & some part
directly in echo.
 You have to use a variable that contains string “PHP
World”.
CLASS PRACTICE
3. Write a program to print 2 php variables using single echo
statement.

 First variable have text “Good Morning.”


 Second variable have text “Have a nice day!”
 Your output should be “Good morning. Have a nice
day!”
 You are allowed to use only one echo statement in
this program.
CLASS PRACTICE

4. Write a program to print “Hello World” using echo


only?
 You can not use any variable.
Thanks!
Any questions?

You can find me at:


yaasiraliboss65@gmail.com
sunriseacademy@outlook.com
www.sunriseacademy.com

You might also like