PHP Variables: Unleash Dynamic Programming! - w9school

Explore the power and versatility of Python variables in our latest blog post. Learn how they enable dynamic programming and boost your coding skills.

PHP Variables: Unleash Dynamic Programming! - w9school

PHP Variables

PHP variables serve as containers that store information, providing labeled boxes where various objects can be kept. Here are a few key facts about PHP variables:

  • Naming Variables: Variables have names. This could include words such as "name," "age," or "color," but they must begin with a $ sign - so instead of just being called by their names alone it would read as: "$name."

  • Storing Information: Variables can be used to store all sorts of information, from names and numbers to whether something is true or false.

  • Types of Information: There are different forms of data variables can store:

             Strings: Words or sentences enclosed within quotation marks. An example would be: "$name = John;"

             Numbers: These can be used for counting. For instance, "$age = 10;" means that one's age is 10.

  • True/False Test: Variables can help you quickly determine whether something is true or false, such as using "$isStudent = true;" to indicate someone who is currently studying in school. Once information has been stored in a variable, you can use it directly within code. For instance, using "$name = "John";", you could say: "Hello John!" using $name in your program code.

  • Change of Information: Variables can be updated at any time; for instance, "$age = 10;" can become "$age = 11;" as someone ages.

  • Where Variables Work: Some variables only operate within certain spaces in your room; for instance, inside of a box. Others, however, work everywhere throughout your house if placed somewhere like in the living room. These are called "local" and "global" variables.

  • Super Special Variables: PHP has some super special boxes that are always there. They help you get information from the outside world. These are called "superglobals." So, in simple words, PHP variables are like labeled containers where you can keep different types of information, and you can use this information in your programs to make things happen.

Creating (Declaring) PHP Variables

In PHP variables, variables begin at an $ sign, then an initial name for the variable.

Example 

$txt = "Hello world! ";
$x = 5;
$y = 10.5;
?>

After executing these statements following the execution of the statements above, after the execution of these statements, $txt's variable will be holding"Hello world!" The Variable $x is holding the value 5 and that variable will contain the number 10.5.

NOTE: When you assign an amount of text for a variable place in quotes the text value.

Consider variables as containers to store information.

NOTE: Unlike other programming languages, PHP has no command to declare the existence of a variable. It is created at the time you assign it a value.

PHP Variables Names

A variable may be given a simple name (like the letters x or y) or an evocative name (age carname, total_volume,).

The PHP variable rules:

  • A variable is identified by an "$" sign and is followed by the name the variable.
  • A variable name must begin with a letter or an underscore character
  • A variable's name can't begin with a number.
  • A variable's name must contain alpha-numeric characters as well as underscores (A-z, 0 as well as _ )
  • Variable names are case sensitive ( $age and $AGE are two distinct variables)

Be aware that PHP variables are case-sensitive!

Output Variables

It is the PHP echo statement is typically used to send data to the screen.

This example will demonstrate what you can do to produce text as an array of variables:

Example

$txt = "W9chool.com";
echo "I love $txt! ";
?>

Try it yourself

In the following instance, you will give identical output to the previous example:

Example

$txt = "W9School.com";
echo "I love " . $txt . "! ";
?>

This example will show the total of two variables:

Example

$x = 5;
$y = 4;
echo $x + $y;
?>

Try it yourself

Notification: You will learn more about the echo statement and the way to display data on display in the following chapter.

PHP is a Loosely Typed Language

In the previous example you will notice that we didn't have to inform PHP what data type the variable belongs to.

PHP automatically associates a particular data type to the variable based on the value. Because the data types do not have to be defined in the strict sense, it is possible to apply things like adding an integer with a string without creating an error.

In PHP 7, declarations of type were added. This allows you to specify the type of data that is required when defining a function and if you enable the requirement to be strict that it throws an "Fatal Error" on a type match.

PHP Variables Scope

In PHP variable declaration, variables can be declared in any script.

The definition of a variable refers to the area of script that allows the variable to be used or referenced.

PHP is a variable PHP that has three areas:

  • Local
  • worldwide
  • static

Global and Local Scope

A variable defined in the absence of of a function is within the GLOBAL scope and is able to be used outside of the function

Example 

Global scope of the variable:

$5 = $x;the global area of

Function myTest() {function myTest()
// Using x within this function can cause an error
echo "

Variable x in function as follows: $x

";
}
myTest();

echo "

Variable x's function outside of it is as follows: $x

";
?>

Try it yourself

A variable defined in an application has an LOCAL SCOPE. It is only accessible within the function in question:

Example

Local scope variable:

Function myTest() {function myTest()
$x = 5; local area of
echo "

Variable x in function the form: $x

";
}
myTest();

If you use x in a way that is outside the function can result in an error
echo "

Variable x's function outside of it is the form: $x

";
?>

Try it yourself

It is possible to have local variables that have the same name within various functions because local variables can only be recognized by the function for the function in which they are defined.

PHP is the global keyword

Global keyword is used to access global variables. global keyword can be used to get access to the global variables inside the function.

In order to do this, add to do this, use the globally keyword in front of your variables (inside the function):

Example

$x = 5;
$y = 10;

MyTest function() {function myTest()
Global $x, $y
$y = $x + $y;
}

myTest();
echo $y * outputs 15
?>

Try it yourself

PHP additionally stores the global variables of all PHP users in an array referred to as $GLOBALS indexindex. This index is information about the variables name. The array can also be accessed via functions, and may be used to modify global variables in a single step.

This example can be modified to read:

Example

$x = 5;
$y = 10;

MyTest function() {function myTest()
$GLOBALS['y'] = $GLOBALS['x'] + $GLOBALS['y'];
}

myTest();
echo $y * outputs 15
?>

Try it yourself

PHP The static Keyword

In general, once a function has been completed or executed, all its variables are removed. Sometimes, we would like to prevent a local variable from be removed. This is to perform a different task.

In order to do this, make use of your static keyword at the time you define the variable.

Example

Function myTest() {function myTest()
Static $x equals 0
echo $x;
$x++;
}

myTest();
myTest();
myTest();
?>

When the function is invoked this variable will be able to hold the data it had at the time that it was called.

Try it yourself

Note: The variable is not yet local to the program.

PHP Excersise

Test Yourself With Exercises

Practice your PHP skills Here 

What's Your Reaction?

like

dislike

love

funny

angry

sad

wow