PHP Variables
Declaring Variables
<?php
# In PHP, variables start with a dollar sign.
$greeting; # $greeting is declared but not defined.
echo $greeting; # Outputting an undefined variable will generate an error message.
?>
# In PHP, variables start with a dollar sign.
$greeting; # $greeting is declared but not defined.
echo $greeting; # Outputting an undefined variable will generate an error message.
?>
Browser Output
Defining Variables
<?php
$greeting = "Hello World!"; # $greeting is both declared and defined.
echo $greeting;
?>
$greeting = "Hello World!"; # $greeting is both declared and defined.
echo $greeting;
?>
Browser Output
Hello World!
Variable Names
<?php
/*
Immediately after the dollar sign the variable must have an underscore character '_', or a letter (no numbers).
Variables, unlike PHP key words, are case sensitive.
The following examples are all different variables.
*/
$_var = 'I am a variable with an underscore!';
$Var = 'I am a variable with a capital letter';
$var = 'I am a new variable all in lowercase';
echo $_var;
echo '<br>';
echo $Var;
echo '<br>';
echo $var;
?>
/*
Immediately after the dollar sign the variable must have an underscore character '_', or a letter (no numbers).
Variables, unlike PHP key words, are case sensitive.
The following examples are all different variables.
*/
$_var = 'I am a variable with an underscore!';
$Var = 'I am a variable with a capital letter';
$var = 'I am a new variable all in lowercase';
echo $_var;
echo '<br>';
echo $Var;
echo '<br>';
echo $var;
?>
Browser Output
I am a variable with an underscore!
I am a variable with a capital letter
I am a new variable all in lowercase
I am a variable with a capital letter
I am a new variable all in lowercase
Simple Data Types
<?php
/*
Variables can hold complex data types but some common simple ones are:
int : A whole number like 3, 7 or 999
float : A number with with decimal places like 2.5 or 1.234
boolean : Can only be true or false
string : A combination of characters within single or double quotes
*/
$int = 11;
echo $int;
echo '<br>';
$float = 3.333;
echo $float;
echo '<br>';
$bool = true;
echo $bool;
echo '<br>';
$string = 'I am a string';
echo $string;
?>
/*
Variables can hold complex data types but some common simple ones are:
int : A whole number like 3, 7 or 999
float : A number with with decimal places like 2.5 or 1.234
boolean : Can only be true or false
string : A combination of characters within single or double quotes
*/
$int = 11;
echo $int;
echo '<br>';
$float = 3.333;
echo $float;
echo '<br>';
$bool = true;
echo $bool;
echo '<br>';
$string = 'I am a string';
echo $string;
?>
Browser Output
11
3.333
1
I am a string
3.333
1
I am a string
Loosely Typed
<?php
/*
PHP is similar to Javascript in that you don't have to declare the data types of variables.
In PHP, variables can hold any data types, simple or complex.
A useful built in PHP function is 'var_dump' which can reveal information about a variable.
*/
$number = 0.75;
$name = 'Gandalf The Grey';
var_dump($number);
echo '<br>';
var_dump($name);
?>
/*
PHP is similar to Javascript in that you don't have to declare the data types of variables.
In PHP, variables can hold any data types, simple or complex.
A useful built in PHP function is 'var_dump' which can reveal information about a variable.
*/
$number = 0.75;
$name = 'Gandalf The Grey';
var_dump($number);
echo '<br>';
var_dump($name);
?>
Browser Output
float(0.75)
string(16) "Gandalf The Grey"
string(16) "Gandalf The Grey"