Home To Know Codes Declare Variables in PHP

Declare Variables in PHP

457
3
SHARE
Declare Variables in PHP

Creating (Declaring) Variables in PHP

Example: 1
<?php
$txt = "Hi einly!";
$x = 5;
$y = 10.5;
echo $txt;
echo "<br>";
echo $x;
echo "<br>";
echo $y;
?>

Output
Hi einly!
5
10.5

How to Output Text and a Variable

Example: 2
<?php
$txt = "Hi einly!";
echo "Hey $txt!";
?>

Output
Hey Hi einly!!

Example: 3
<?php
$txt = "Hi einly!";
echo "Hey " . $txt . "!"
?>

Output
Hey Hi einly!!

Sum of two variables

Example: 4
<?php
$a = 10;
$b = 15;
echo $a+$b;
?>

Output
25

PHP has three different variable scopes:

  1. Local
  2. Global
  3. Static

Global and Local Scope

Define variable global still not use in inside function

Example: 5
<?php
$a = 10; // variable global scope
function First() {
// using a inside this function will generate an error
echo "<p>Variable a inside function is: $a</p>";
}
First();
echo "<p>Variable a outside function is: $a</p>";
?>

Output

Global Variable Example Output
Global Variable Example Output

Define variable locally inside function which can’t be access outside

Example: 6
<?php
function First() {
$a = 10; // variable local scope
// using a outside this function will generate an error
echo "<p>Variable a inside function is: $a</p>";
}
First();
echo "<p>Variable a outside function is: $a</p>";
?>

Output

Local Variable Example Output
Local Variable Example Output

Global keyword used in Global variable

Example: 7
<?php
$a = 10; // variable global scope
function First() {
global $a;
echo "<p>Variable an inside function is: $a</p>";
}
First();
echo "<p>Variable an outside function is: $a</p>";
?>

Output
Variable an inside function is: 10
Variable an outside function is: 10

PHP also stores all global variables in an array called $GLOBALS[index]. The index holds the name of the variable. This array is also accessible from within functions and can be used to update global variables directly.

Example: 8
<?php
$a = 10; // variable global scope
function First() {
$GLOBALS['a'] = $GLOBALS['a'] + $GLOBALS['a'];
}
First();
echo "<p>Variable an outside function is: $a</p>";
?>

Output
Variable an outside function is: 20

Static Keyword

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.

Example: 9
<?php
function First() {
static $a = 0;
echo $a.'<br>';
$a++;
}
First();
First();
First();";
?>

Output
0
1
2

3 COMMENTS

  1. I’ve read several just right stuff here. Definitely value bookmarking for revisiting. I surprise how much attempt you place to make this kind of excellent informative website.

  2. I think other website proprietors should take this web site as an model, very clean and wonderful user friendly style and design, as well as the content. You’re an expert in this topic!

  3. I really like your blog.. very nice colors & theme. Did you design this website yourself or did you hire someone to do it for you? Plz reply as I’m looking to design my own blog and would like to know where u got this from. thanks

Leave a Reply