How to declare variable in Php
Php Variables: Variables is a name of storage space which is used to store data. Its value may be changed. It always contains the last value stored to it. Variables is a container which holds the value. Variable in PHP starts with dollar($) sign.
Variables are tools used to store information for later use. Think of variables as placeholders for any amount of data. This data Can then be used throughout your
code with ease. Variables are like the touchscreens of our modern devices, you can access the data with your fingertips. Just like touchscreens, variables offer a direct means of interaction with the information that is stored within them.
- We do not need to declare the data types of the variables because PHP is a weakly typed language.
- It evaluates the values automatically and converts them to the appropriate datatype.
- A variable can be utilised repeatedly after being declared in the code.
- To assign a value to a variable, use the assignment operator (=).
Best PHP Variables Learn| Types Variables| Declare Variables |
$variablename=value; |
What are variables in PHP, explain with example?
<?php $x=100; $x=50; $x=250; echo "The value of x is:".$x; ?> **********OUTPUT********** The value of x is:250 |
<?php $str="Learn Coding"; echo "I like $str"; ?> **********OUTPUT********** I like Learn Coding |
<?php $str="Learn Coding"; echo "I like ".$str; ?> **********OUTPUT********** I like Learn Coding |
What are the rules to declare variable in PHP?
1. It must start with the dollar sign.
2. There is no need of data type (int, float, char) to define a variable.
3. Variables can, but do not need, to be declared before assignment.
4. After the dollar sign, the first letter of a variable should be alphabet or underscore(_).
5. After the dollar sign, the first letter of a variable should not be a digit.
6. After the first character, it may be a combination of alphabets and digits.
7. Blank spaces are not allowed in variable name.
8. A variable must start with a dollar ($) sign, followed by the variable name.
9. It can only contain alphanumeric character and underscore (A-z, 0-9, _).
10. A variable name must start with a letter or underscore (_) character.
11. A PHP variable name cannot contain spaces.
12. One thing to be kept in mind that the variable name cannot start with a number or special symbols.
13. PHP variables are case-sensitive, so $name and $NAME both are treated as different variable.
PHP Variable: Declaring string, integer, and float
Let's see the example to store string, integer, and float values in PHP variables.
<?php $str="Learn Coding"; $x=100; $y=48.6; echo"string is: $str <br/>"; echo "integer is: $x <br/>"; echo "float is: $y <br/>"; ?> **********OUTPUT********** string is: Learn Coding integer is: 100 float is: 48.6 |
PHP Variable: Sum of two variables
<?php $x=100; $y=200; $z=$x+$y; echo("Addition of two number:".$z); ?> **********OUTPUT********** Addition of two number:300 |
What are the four types of variable scopes?
What is a local variable in PHP?
<?php function local_var() { $num = 45; //local variable echo "Local variable declared inside the function is: ". $num; } local_var(); ?> **********OUTPUT********** Local variable declared inside the function is: 45 |
How to declare local variable in PHP
<?php function mydata() { $x=100; $y=200; echo "The value of x is $x and y is $y"; } mydata(); ?> **********OUTPUT********** The value of x is 100 and y is 200 |
What is the best way to handle global variables in PHP
Global variable: A variable which is declared outside the body of the method or constructor is called a global variable. The global variables are the variables that are declared outside the function.
These variables can be accessed anywhere in the program. To access the global variable within a function, use the GLOBAL keyword before the variable.
However, these variables can be directly accessed or used outside the function without any keyword. Therefore, there is no need to use any keyword to access a global variable outside the function.
Php global variable in function
<?php $x=100; $y=200; function mydata() { $x=10; $y=20; echo "<b>Inside function:</b> The value of x is $x and y is $y <br>"; } mydata(); echo "<b>Outside function:</b>The value of x is $x and y is $y "; ?> **********OUTPUT********** Inside function: The value of x is 10 and y is 20 Outside function:The value of x is 100 and y is 200 |
<?php $name = "Ravi Kumar"; //Global Variable function global_var() { global $name; echo "Variable inside the function: ". $name; echo "</br>"; } global_var(); echo "Variable outside the function: ". $name; ?> **********OUTPUT********** Variable inside the function: Ravi Kumar Variable outside the function: Ravi Kumar |
Note: Without using the global keyword, if you try to access a global variable inside the function, it will generate an error that the variable is undefined.
What is a static variable in PHP
<?php function mydata() { $x=100; echo "$x<br>"; $x++; } mydata(); mydata(); mydata(); ?> **********OUTPUT********** 100 100 100 |
<?php function myFunction() { static $x=10; echo "$x<br>"; $x++; } mydata(); mydata(); mydata(); ?> **********OUTPUT********** 10 11 12 |
<?php function static_var() { static $num1 = 3; //static variable $num2 = 6; //Non-static variable
$num1++;
$num2++; echo "Static: " .$num1 ."</br>"; echo "Non-static: " .$num2 ."</br>"; } static_var(); static_var(); ?> **********OUTPUT********** Static: 4 Non-static: 7 Static: 5 Non-static: 7 |
How can use local variable as global in PHP?
<?php $x=100; $y=50; function mydata() { global $x,$y; echo "<b>Inside function:</b> The value of x is $x and y is $y <br>"; } mydata(); echo "<b>Outside function:</b>The value of x is $x and y is $y "; ?> **********OUTPUT********** Inside function: The value of x is 100 and y is 50 Outside function:The value of x is 100 and y is 50 |
PHP Variable Scope|Local Scope Vs Global Scope Variables
<?php $x=50; $y=50; function mydata() { $sum=$GLOBALS['x']+$GLOBALS['y']; echo "<b>Inside function:</b> Sum=$sum"; } mydata(); $sum=$x+$y; echo "<b>Outside function:</b> Sum=$sum"; ?> **********OUTPUT********** Inside function: Sum=100 Outside function: Sum=100 |
What is PHP $ and $$ variables?
<?php $num1 = 50; //global variable $num2 = 10; //global variable function global_data() { $sum = $GLOBALS['num1'] + $GLOBALS['num2']; echo "The Sum of global variables is:" .$sum; } global_data(); ?> **********OUTPUT********** The sum of global variables is: 60 |
<?php $x = 50; function mydata() { $x = 100; echo "value of x: " .$x; } mydata(); ?> **********OUTPUT********** Value of x: 100 |
PHP $ and $$ Variables: The $var (single dollar) is a normal variable with the name var that stores any value like string, integer, float, etc.
The $$var (double dollar) is a reference variable that stores the value of the $variable inside it.
How to use $ and && in PHP Example
<?php $x = "Learn Coding"; $$x = 500; echo $x."<br/>"; echo $$x."<br/>"; echo $abc; ?> **********OUTPUT********** Learn Coding 500 500 |
0 Comments