SlideShare une entreprise Scribd logo
1  sur  46
 Variables

are "containers" for storing
information.
 PHP variables are Case sensitive.
 PHP Variables Like Algebra:
 x=5
y=6
z=x+y
 In algebra we use letters (like x) to hold values
(like 5).
 From the expression z=x+y above, we can
calculate the value of z to be 11.
 In PHP these letters are called variables.
 <!DOCTYPE

<html>
<body>
<?php
$x=5;
$y=6;
$z=$x+$y;
echo $z;
?>
</body>
</html>
 OUTPUT:
 11

html>
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 alphanumeric characters and underscores (A-z, 0-9,
and _ )
 Variable names are case sensitive ($y and $Y
are two different variables)
 <!DOCTYPE

html>

<html>
<body>
<?php
$txt="Hello world!";
$x=5;
$y=10.5;
echo $txt;
echo "<br>";
echo $x;
echo "<br>";
echo $y;
?>
</body>
</html>
 Hello

5
10.5

world!
 After

the execution of the statements above,
the variable txt will hold the value Hello
world!, the variable x will hold the value 5, and
the variable y will hold the value 10.5.

 Note:

When you assign a text value to a
variable, put quotes around the value.
 PHP

automatically converts the variable to the
correct data type, depending on its value.
 PHP

has three different variable scopes:

local
global

static
A

variable declared outside a function has a
GLOBAL SCOPE and can only be accessed
outside a function.

A

variable declared within a function has a
LOCAL SCOPE and can only be accessed
within that function.


<!DOCTYPE html>
<html>
<body>
<?php
$x=5; // global scope



function myTest()
{
$y=10; // local scope
echo "<p>Test variables inside the function:<p>";
echo "Variable x is: $x";
echo "<br>";
Echo $x;
echo "Variable y is: $y";
}
myTest();
echo "<p>Test variables outside the function:<p>";
echo "Variable x is: $x";
echo "<br>";
echo "Variable y is: $y";
?>

</body>
</html>
 Test

variables inside the function:
 Variable x is:
Variable y is: 10
 Test variables outside the function:
 Variable x is: 5
Variable y is:
 In

the example above there are two variables $x
and $y and a function myTest(). $x is a global
variable since it is declared outside the function
and $y is a local variable since it is created inside
the function.
 When we output the values of the two variables
inside the myTest() function, it prints the value of
$y as it is the locally declared, but cannot print the
value of $x since it is created outside the function.
 Then, when we output the values of the two
variables outside the myTest() function, it prints
the value of $x, but cannot print the value of $y
since it is a local variable and it is created inside
the myTest() function.
 The

global keyword is used to access a global
variable from within a function.
 <?php
$x=5;
$y=10;
function myTest()
{
global $x,$y;
$y=$x+$y;
}

myTest();
echo $y; // outputs 15
?>
 Normally,

when a function is
completed/executed, all of its variables are
deleted. However, sometimes we want a local
variable NOT to be deleted. We need it for a
further job.
 To do this, use the static keyword when you
first declare the variable:


<!DOCTYPE html>
<html>
<body>
<?php

function myTest()
{
static $x=0;
echo $x;
$x++;
}
myTest();
echo "<br>";
myTest();
echo "<br>";
myTest();
echo "<br>";
myTest();
echo "<br>";
myTest();
?>
</body>
</html>
0

1
2
3
4
A

constant is an identifier (name) for a simple
value. The value cannot be changed during the
script.
 A valid constant name starts with a letter or
underscore (no $ sign before the constant
name).
 Note: Unlike variables, constants are
automatically global across the entire script.
 To

set a constant, use the define() function - it
takes three parameters: The first parameter
defines the name of the constant, the second
parameter defines the value of the constant,
and the optional third parameter specifies
whether the constant name should be caseinsensitive. Default is false.
<!DOCTYPE html>
<html>
<body>
<?php
// define a case-sensitive constant
define("GREETING", "Welcome to W3Schools.com!");
echo GREETING;
echo "<br>";
// will not output the value of the constant
echo greeting;
?>
</body>
</html>
 OUTPUT:
 Welcome to W3Schools.com!
greeting

<!DOCTYPE html>
<html>
<body>
<?php
// define a case-insensitive constant
define("GREETING", "Welcome to W3Schools.com!",
true);
echo GREETING;
echo "<br>";
// will also output the value of the constant
echo greeting;
?>
</body>
</html>
 Output:
 Welcome to W3Schools.com!
Welcome to W3Schools.com!

 String,

Integer, Floating point numbers,
Boolean, Array, Object, NULL.
A

string is a sequence of characters like “
welcome to open source software”.
 A string can be any text inside quotes. You can
use single or double quotes:
 <?php

$x = "Hello world!";
echo $x;
echo "<br>";
$x = 'Hello world!';
echo $x;
?>
OUTPUT:
 Hello world!
Hello world!
 An

integer is a number without decimals.
 Rules for integers:
 An integer must have at least one digit (0-9)
 An integer cannot contain comma or blanks
 An integer must not have a decimal point
 An integer can be either positive or negative


<!DOCTYPE html>
<html>
<body>
<?php
$x = 5985;
var_dump($x);
echo "<br>";
$x = -345; // negative number
var_dump($x);
echo "<br>";
$x = 0x8C; // hexadecimal number
var_dump($x);
echo "<br>";
$x = 047; // octal number
var_dump($x);
?>
</body>
</html>
 int(5985)

int(-345)
int(140)
int(39)
A

floating point number is a number with a
decimal point or a number in exponential form.
 The PHP var_dump() function returns the data
type and value of variables:
 <?php

$x = 10.365;
var_dump($x);
echo "<br>";
$x = 2.4e3;
var_dump($x);
echo "<br>";
$x = 8E-5;
var_dump($x);
?>
 OUTPUT:
 float(10.365)
float(2400)
float(8.0E-5)
 Booleans

can be either TRUE or FALSE.

 $x=true;

$y=false;
 Booleans are often used in conditional
testing.
Operator

Name

Example

Result

+

Addition

$x + $y

Sum of $x and $y

-

Subtraction

$x - $y

Difference of $x
and $y

*

Multiplication

$x * $y

Product of $x and
$y

/

Division

$x / $y

Quotient of $x and
$y

%

Modulus

$x % $y

Remainder of $x
divided by $y
 <?php

$x=10;
$y=6;
echo ($x + $y);
echo "<br>";
echo ($x - $y);
echo "<br>";
echo ($x * $y);
echo "<br>";
echo ($x / $y);
echo "<br>";
echo ($x % $y);
?>
 16

4
60
1.6666666666667
4
Operator

Name

Example

Result

.

Concatenation

$txt1 = "Hello"
$txt2 = $txt1 . "
world!"

Now $txt2
contains "Hello
world!"

.=

Concatenation
assignment

$txt1 = "Hello"
Now $txt1
$txt1 .= " world!" contains "Hello
world!"
 <?php

$a = "Hello";
$b = $a . " world!";
echo $b; // outputs Hello world!
echo "<br>";
$x="Hello";
$x .= " world!";
echo $x; // outputs Hello world!
?>
 Hello

world!
Hello world!
erator

Name

Description

++$x

Pre-increment

Increments $x by one,
then returns $x

$x++

Post-increment

Returns $x, then
increments $x by one

--$x

Pre-decrement

Decrements $x by one,
then returns $x

$x--

Post-decrement

Returns $x, then
decrements $x by one
<?php
$x=10;
echo ++$x;
echo "<br>";
$y=10;
echo $y++;
echo "<br>";
$z=5;
echo --$z;
echo "<br>";

$i=5;
echo $i--;
?>
OUTPUT:
11
10
4
5
Operator

Name

Example

Result

==

Equal

$x == $y

True if $x is equal
to $y

===

Identical

$x === $y

True if $x is equal
to $y, and they
are of the same
type

!=

Not equal

$x != $y

True if $x is not
equal to $y

<>

Not equal

$x <> $y

True if $x is not
equal to $y

!==

Not identical

$x !== $y

True if $x is not
equal to $y, or
they are not of
the same type

>

Greater than

$x > $y

True if $x is
greater than $y

<

Less than

$x < $y

True if $x is less


<?php
$x=100;
$y="100";
var_dump($x == $y); // returns true because values are equal
echo "<br>";
var_dump($x === $y); // returns false because types are not equal
echo "<br>";
var_dump($x != $y); // returns false because values are equal
echo "<br>";
var_dump($x !== $y); // returns true because types are not equal
echo "<br>";
$a=50;
$b=90;
var_dump($a > $b);
echo "<br>";
var_dump($a < $b);
?>
PHP  variables
PHP  variables
PHP  variables
PHP  variables
PHP  variables
PHP  variables

Contenu connexe

Tendances (20)

Javascript
JavascriptJavascript
Javascript
 
HTML Forms
HTML FormsHTML Forms
HTML Forms
 
PHP FUNCTIONS
PHP FUNCTIONSPHP FUNCTIONS
PHP FUNCTIONS
 
Php.ppt
Php.pptPhp.ppt
Php.ppt
 
Oops concepts in php
Oops concepts in phpOops concepts in php
Oops concepts in php
 
File in C language
File in C languageFile in C language
File in C language
 
Php with MYSQL Database
Php with MYSQL DatabasePhp with MYSQL Database
Php with MYSQL Database
 
PHP - DataType,Variable,Constant,Operators,Array,Include and require
PHP - DataType,Variable,Constant,Operators,Array,Include and requirePHP - DataType,Variable,Constant,Operators,Array,Include and require
PHP - DataType,Variable,Constant,Operators,Array,Include and require
 
Loops PHP 04
Loops PHP 04Loops PHP 04
Loops PHP 04
 
MYSQL - PHP Database Connectivity
MYSQL - PHP Database ConnectivityMYSQL - PHP Database Connectivity
MYSQL - PHP Database Connectivity
 
Database Connectivity in PHP
Database Connectivity in PHPDatabase Connectivity in PHP
Database Connectivity in PHP
 
Class 3 - PHP Functions
Class 3 - PHP FunctionsClass 3 - PHP Functions
Class 3 - PHP Functions
 
Class 5 - PHP Strings
Class 5 - PHP StringsClass 5 - PHP Strings
Class 5 - PHP Strings
 
Introduction to php
Introduction to phpIntroduction to php
Introduction to php
 
Php forms
Php formsPhp forms
Php forms
 
Html forms
Html formsHtml forms
Html forms
 
4.2 PHP Function
4.2 PHP Function4.2 PHP Function
4.2 PHP Function
 
Javascript arrays
Javascript arraysJavascript arrays
Javascript arrays
 
Operators php
Operators phpOperators php
Operators php
 
PHP
PHPPHP
PHP
 

En vedette (20)

PHP
PHPPHP
PHP
 
Php Presentation
Php PresentationPhp Presentation
Php Presentation
 
Php tutorial
Php tutorialPhp tutorial
Php tutorial
 
Php variables (english)
Php variables (english)Php variables (english)
Php variables (english)
 
Control Structures In Php 2
Control Structures In Php 2Control Structures In Php 2
Control Structures In Php 2
 
What's new in PHP 7.1
What's new in PHP 7.1What's new in PHP 7.1
What's new in PHP 7.1
 
Cookie and session
Cookie and sessionCookie and session
Cookie and session
 
Php Ppt
Php PptPhp Ppt
Php Ppt
 
Cookies and sessions
Cookies and sessionsCookies and sessions
Cookies and sessions
 
Introduction to PHP
Introduction to PHPIntroduction to PHP
Introduction to PHP
 
MySQL Built-In Functions
MySQL Built-In FunctionsMySQL Built-In Functions
MySQL Built-In Functions
 
PHP - Introduction to String Handling
PHP -  Introduction to  String Handling PHP -  Introduction to  String Handling
PHP - Introduction to String Handling
 
Php Using Arrays
Php Using ArraysPhp Using Arrays
Php Using Arrays
 
Virtualization Concepts
Virtualization ConceptsVirtualization Concepts
Virtualization Concepts
 
Chapter 02 php basic syntax
Chapter 02   php basic syntaxChapter 02   php basic syntax
Chapter 02 php basic syntax
 
PHP Presentation
PHP PresentationPHP Presentation
PHP Presentation
 
Php hypertext pre-processor
Php   hypertext pre-processorPhp   hypertext pre-processor
Php hypertext pre-processor
 
Basic networking
Basic networkingBasic networking
Basic networking
 
Xampp Ppt
Xampp PptXampp Ppt
Xampp Ppt
 
Font
FontFont
Font
 

Similaire à PHP variables

Similaire à PHP variables (20)

PHP Basics
PHP BasicsPHP Basics
PHP Basics
 
Php Tutorials for Beginners
Php Tutorials for BeginnersPhp Tutorials for Beginners
Php Tutorials for Beginners
 
php AND MYSQL _ppt.pdf
php AND MYSQL _ppt.pdfphp AND MYSQL _ppt.pdf
php AND MYSQL _ppt.pdf
 
PHP Basic
PHP BasicPHP Basic
PHP Basic
 
07 Introduction to PHP #burningkeyboards
07 Introduction to PHP #burningkeyboards07 Introduction to PHP #burningkeyboards
07 Introduction to PHP #burningkeyboards
 
Php introduction
Php introductionPhp introduction
Php introduction
 
Advanced php
Advanced phpAdvanced php
Advanced php
 
Php using variables-operators
Php using variables-operatorsPhp using variables-operators
Php using variables-operators
 
OpenGurukul : Language : PHP
OpenGurukul : Language : PHPOpenGurukul : Language : PHP
OpenGurukul : Language : PHP
 
Web 8 | Introduction to PHP
Web 8 | Introduction to PHPWeb 8 | Introduction to PHP
Web 8 | Introduction to PHP
 
Javascript variables and datatypes
Javascript variables and datatypesJavascript variables and datatypes
Javascript variables and datatypes
 
Learn PHP Basics
Learn PHP Basics Learn PHP Basics
Learn PHP Basics
 
Expressions and Operators.pptx
Expressions and Operators.pptxExpressions and Operators.pptx
Expressions and Operators.pptx
 
PHP PPT FILE
PHP PPT FILEPHP PPT FILE
PHP PPT FILE
 
Php + my sql
Php + my sqlPhp + my sql
Php + my sql
 
Php & my sql
Php & my sqlPhp & my sql
Php & my sql
 
php.pdf
php.pdfphp.pdf
php.pdf
 
Php
PhpPhp
Php
 
Vision academy classes bcs_bca_bba_sybba_php
Vision academy  classes bcs_bca_bba_sybba_phpVision academy  classes bcs_bca_bba_sybba_php
Vision academy classes bcs_bca_bba_sybba_php
 
FYBSC IT Web Programming Unit IV PHP and MySQL
FYBSC IT Web Programming Unit IV  PHP and MySQLFYBSC IT Web Programming Unit IV  PHP and MySQL
FYBSC IT Web Programming Unit IV PHP and MySQL
 

Plus de Siddique Ibrahim (20)

List in Python
List in PythonList in Python
List in Python
 
Python Control structures
Python Control structuresPython Control structures
Python Control structures
 
Python programming introduction
Python programming introductionPython programming introduction
Python programming introduction
 
Data mining basic fundamentals
Data mining basic fundamentalsData mining basic fundamentals
Data mining basic fundamentals
 
Networking devices(siddique)
Networking devices(siddique)Networking devices(siddique)
Networking devices(siddique)
 
Osi model 7 Layers
Osi model 7 LayersOsi model 7 Layers
Osi model 7 Layers
 
Mysql grand
Mysql grandMysql grand
Mysql grand
 
Getting started into mySQL
Getting started into mySQLGetting started into mySQL
Getting started into mySQL
 
pipelining
pipeliningpipelining
pipelining
 
Micro programmed control
Micro programmed controlMicro programmed control
Micro programmed control
 
Hardwired control
Hardwired controlHardwired control
Hardwired control
 
interface
interfaceinterface
interface
 
Interrupt
InterruptInterrupt
Interrupt
 
Interrupt
InterruptInterrupt
Interrupt
 
DMA
DMADMA
DMA
 
Io devies
Io deviesIo devies
Io devies
 
Stack & queue
Stack & queueStack & queue
Stack & queue
 
Metadata in data warehouse
Metadata in data warehouseMetadata in data warehouse
Metadata in data warehouse
 
Data extraction, transformation, and loading
Data extraction, transformation, and loadingData extraction, transformation, and loading
Data extraction, transformation, and loading
 
Aggregate fact tables
Aggregate fact tablesAggregate fact tables
Aggregate fact tables
 

Dernier

HTML Injection Attacks: Impact and Mitigation Strategies
HTML Injection Attacks: Impact and Mitigation StrategiesHTML Injection Attacks: Impact and Mitigation Strategies
HTML Injection Attacks: Impact and Mitigation StrategiesBoston Institute of Analytics
 
What Are The Drone Anti-jamming Systems Technology?
What Are The Drone Anti-jamming Systems Technology?What Are The Drone Anti-jamming Systems Technology?
What Are The Drone Anti-jamming Systems Technology?Antenna Manufacturer Coco
 
Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...Enterprise Knowledge
 
Automating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps ScriptAutomating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps Scriptwesley chun
 
Data Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonData Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonAnna Loughnan Colquhoun
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerThousandEyes
 
[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdfhans926745
 
Boost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityBoost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityPrincipled Technologies
 
Exploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone ProcessorsExploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone Processorsdebabhi2
 
Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)wesley chun
 
2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...Martijn de Jong
 
Strategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a FresherStrategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a FresherRemote DBA Services
 
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Miguel Araújo
 
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationFrom Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationSafe Software
 
Histor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slideHistor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slidevu2urc
 
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemkeProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemkeProduct Anonymous
 
A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?Igalia
 
Partners Life - Insurer Innovation Award 2024
Partners Life - Insurer Innovation Award 2024Partners Life - Insurer Innovation Award 2024
Partners Life - Insurer Innovation Award 2024The Digital Insurer
 
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...apidays
 

Dernier (20)

HTML Injection Attacks: Impact and Mitigation Strategies
HTML Injection Attacks: Impact and Mitigation StrategiesHTML Injection Attacks: Impact and Mitigation Strategies
HTML Injection Attacks: Impact and Mitigation Strategies
 
What Are The Drone Anti-jamming Systems Technology?
What Are The Drone Anti-jamming Systems Technology?What Are The Drone Anti-jamming Systems Technology?
What Are The Drone Anti-jamming Systems Technology?
 
Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...
 
Automating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps ScriptAutomating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps Script
 
Data Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonData Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt Robison
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected Worker
 
[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf
 
Boost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityBoost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivity
 
Exploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone ProcessorsExploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone Processors
 
Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)
 
2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...
 
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
 
Strategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a FresherStrategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a Fresher
 
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
 
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationFrom Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
 
Histor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slideHistor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slide
 
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemkeProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
 
A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?
 
Partners Life - Insurer Innovation Award 2024
Partners Life - Insurer Innovation Award 2024Partners Life - Insurer Innovation Award 2024
Partners Life - Insurer Innovation Award 2024
 
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
 

PHP variables

  • 1.
  • 2.  Variables are "containers" for storing information.  PHP variables are Case sensitive.  PHP Variables Like Algebra:  x=5 y=6 z=x+y  In algebra we use letters (like x) to hold values (like 5).  From the expression z=x+y above, we can calculate the value of z to be 11.  In PHP these letters are called variables.
  • 4. 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 alphanumeric characters and underscores (A-z, 0-9, and _ )  Variable names are case sensitive ($y and $Y are two different variables)
  • 5.  <!DOCTYPE html> <html> <body> <?php $txt="Hello world!"; $x=5; $y=10.5; echo $txt; echo "<br>"; echo $x; echo "<br>"; echo $y; ?> </body> </html>
  • 7.  After the execution of the statements above, the variable txt will hold the value Hello world!, the variable x will hold the value 5, and the variable y will hold the value 10.5.  Note: When you assign a text value to a variable, put quotes around the value.
  • 8.  PHP automatically converts the variable to the correct data type, depending on its value.
  • 9.  PHP has three different variable scopes: local global static
  • 10. A variable declared outside a function has a GLOBAL SCOPE and can only be accessed outside a function. A variable declared within a function has a LOCAL SCOPE and can only be accessed within that function.
  • 11.  <!DOCTYPE html> <html> <body> <?php $x=5; // global scope  function myTest() { $y=10; // local scope echo "<p>Test variables inside the function:<p>"; echo "Variable x is: $x"; echo "<br>"; Echo $x; echo "Variable y is: $y"; } myTest(); echo "<p>Test variables outside the function:<p>"; echo "Variable x is: $x"; echo "<br>"; echo "Variable y is: $y"; ?> </body> </html>
  • 12.  Test variables inside the function:  Variable x is: Variable y is: 10  Test variables outside the function:  Variable x is: 5 Variable y is:
  • 13.  In the example above there are two variables $x and $y and a function myTest(). $x is a global variable since it is declared outside the function and $y is a local variable since it is created inside the function.  When we output the values of the two variables inside the myTest() function, it prints the value of $y as it is the locally declared, but cannot print the value of $x since it is created outside the function.  Then, when we output the values of the two variables outside the myTest() function, it prints the value of $x, but cannot print the value of $y since it is a local variable and it is created inside the myTest() function.
  • 14.  The global keyword is used to access a global variable from within a function.  <?php $x=5; $y=10; function myTest() { global $x,$y; $y=$x+$y; } myTest(); echo $y; // outputs 15 ?>
  • 15.  Normally, when a function is completed/executed, all of its variables are deleted. However, sometimes we want a local variable NOT to be deleted. We need it for a further job.  To do this, use the static keyword when you first declare the variable:
  • 16.  <!DOCTYPE html> <html> <body> <?php function myTest() { static $x=0; echo $x; $x++; } myTest(); echo "<br>"; myTest(); echo "<br>"; myTest(); echo "<br>"; myTest(); echo "<br>"; myTest(); ?> </body> </html>
  • 18. A constant is an identifier (name) for a simple value. The value cannot be changed during the script.  A valid constant name starts with a letter or underscore (no $ sign before the constant name).  Note: Unlike variables, constants are automatically global across the entire script.
  • 19.  To set a constant, use the define() function - it takes three parameters: The first parameter defines the name of the constant, the second parameter defines the value of the constant, and the optional third parameter specifies whether the constant name should be caseinsensitive. Default is false.
  • 20. <!DOCTYPE html> <html> <body> <?php // define a case-sensitive constant define("GREETING", "Welcome to W3Schools.com!"); echo GREETING; echo "<br>"; // will not output the value of the constant echo greeting; ?> </body> </html>  OUTPUT:  Welcome to W3Schools.com! greeting 
  • 21. <!DOCTYPE html> <html> <body> <?php // define a case-insensitive constant define("GREETING", "Welcome to W3Schools.com!", true); echo GREETING; echo "<br>"; // will also output the value of the constant echo greeting; ?> </body> </html>  Output:  Welcome to W3Schools.com! Welcome to W3Schools.com! 
  • 22.  String, Integer, Floating point numbers, Boolean, Array, Object, NULL.
  • 23. A string is a sequence of characters like “ welcome to open source software”.  A string can be any text inside quotes. You can use single or double quotes:
  • 24.  <?php $x = "Hello world!"; echo $x; echo "<br>"; $x = 'Hello world!'; echo $x; ?> OUTPUT:  Hello world! Hello world!
  • 25.  An integer is a number without decimals.  Rules for integers:  An integer must have at least one digit (0-9)  An integer cannot contain comma or blanks  An integer must not have a decimal point  An integer can be either positive or negative
  • 26.  <!DOCTYPE html> <html> <body> <?php $x = 5985; var_dump($x); echo "<br>"; $x = -345; // negative number var_dump($x); echo "<br>"; $x = 0x8C; // hexadecimal number var_dump($x); echo "<br>"; $x = 047; // octal number var_dump($x); ?> </body> </html>
  • 28. A floating point number is a number with a decimal point or a number in exponential form.  The PHP var_dump() function returns the data type and value of variables:
  • 29.  <?php $x = 10.365; var_dump($x); echo "<br>"; $x = 2.4e3; var_dump($x); echo "<br>"; $x = 8E-5; var_dump($x); ?>  OUTPUT:  float(10.365) float(2400) float(8.0E-5)
  • 30.  Booleans can be either TRUE or FALSE.  $x=true; $y=false;  Booleans are often used in conditional testing.
  • 31. Operator Name Example Result + Addition $x + $y Sum of $x and $y - Subtraction $x - $y Difference of $x and $y * Multiplication $x * $y Product of $x and $y / Division $x / $y Quotient of $x and $y % Modulus $x % $y Remainder of $x divided by $y
  • 32.  <?php $x=10; $y=6; echo ($x + $y); echo "<br>"; echo ($x - $y); echo "<br>"; echo ($x * $y); echo "<br>"; echo ($x / $y); echo "<br>"; echo ($x % $y); ?>
  • 34. Operator Name Example Result . Concatenation $txt1 = "Hello" $txt2 = $txt1 . " world!" Now $txt2 contains "Hello world!" .= Concatenation assignment $txt1 = "Hello" Now $txt1 $txt1 .= " world!" contains "Hello world!"
  • 35.  <?php $a = "Hello"; $b = $a . " world!"; echo $b; // outputs Hello world! echo "<br>"; $x="Hello"; $x .= " world!"; echo $x; // outputs Hello world! ?>
  • 37. erator Name Description ++$x Pre-increment Increments $x by one, then returns $x $x++ Post-increment Returns $x, then increments $x by one --$x Pre-decrement Decrements $x by one, then returns $x $x-- Post-decrement Returns $x, then decrements $x by one
  • 38. <?php $x=10; echo ++$x; echo "<br>"; $y=10; echo $y++; echo "<br>"; $z=5; echo --$z; echo "<br>"; $i=5; echo $i--; ?> OUTPUT: 11 10 4 5
  • 39. Operator Name Example Result == Equal $x == $y True if $x is equal to $y === Identical $x === $y True if $x is equal to $y, and they are of the same type != Not equal $x != $y True if $x is not equal to $y <> Not equal $x <> $y True if $x is not equal to $y !== Not identical $x !== $y True if $x is not equal to $y, or they are not of the same type > Greater than $x > $y True if $x is greater than $y < Less than $x < $y True if $x is less
  • 40.  <?php $x=100; $y="100"; var_dump($x == $y); // returns true because values are equal echo "<br>"; var_dump($x === $y); // returns false because types are not equal echo "<br>"; var_dump($x != $y); // returns false because values are equal echo "<br>"; var_dump($x !== $y); // returns true because types are not equal echo "<br>"; $a=50; $b=90; var_dump($a > $b); echo "<br>"; var_dump($a < $b); ?>