Variables In PHP

This post describes PHP Variables in a way that is in compliance with educational standards.

A variable can be thought of as a “container” for storing information.



PHP Variables

PHP Variables can be named in two ways: short and descriptive (example a and b), and long and descriptive (example: name, greeting, area_surface).

A variable in PHP is created by starting with a $ sign, followed by the variable’s name.

For example:

Example: 

<?php $name = "Bill Gates!"; $a = 3; $b = 6.2; ?>

Example: 

<?php $string = "Bill Gates is the co-founder of Microsoft"; $l = 18.2; $m = 7; ?>

As a result of executing the above statements, the variable will be:

$name holds the value of Bill Gates!, $a holds the value of 3, And variable $b holds the value of 6.2.

Remember: Put quotes around text values when assigning them to variables.

IMPORTANT: PHP does not have a command for declaring variables. As soon as you assign a value to it, it is created.

A variable is a container for storing data.


PHP Variable Output

To display data on a screen, PHP echo is often used.

Below is an example showing how to display text and a variable:

Example: 

<?php $link = "facebook.com"; echo "I love $link!"; ?>

Example: 

<?php $name = "Bill Gates"; echo "This is $name, from microsoft!"; ?>

Similar to the above example, this example will produce the following output:

Example: 

<?php $link = "facebook.com"; echo "I love" . $link . "!"; ?>

Example: 

<?php $name = "Bill Gates"; echo "This is " . $name . ", from microsoft!"; ?>

Here is a simple example that sums two variables:

Example: 

<?php $a = 3; $b = 8; echo $a + $b; ?>

The example above demonstrates that PHP did not require us to specify the variable’s data type.

Depending on the variable’s value, PHP automatically assigns a data type.

It is possible to add a string to an integer without causing an error because the data types are not strictly defined.

Type declarations were added to PHP 7. If a type mismatch occurs when declaring a function, the strict requirement will throw a “Fatal Error”.

Remember: In the next chapter, you’ll learn more about how to output data to the screen using the echo statement.


PHP Variables Key Points

When declaring a variable in PHP, remember the following key points:

  1. Variable names begin with the $ sign, followed by the variable name.
  2. There must be a letter or underscore at the beginning of a variable name.
  3. Variable names cannot begin with a number.
  4. There can be only alphanumeric characters and underscores in variable names (A-z, 0-9, and _).
  5. It is recommended to use descriptive and meaningful names for variables to improve code readability and maintainability.
  6. The names of variables are case-sensitive ($name and $NAME are two different variables).
  7. Variables are assigned values using the “=” operator.
  8. The value of a variable can be changed throughout the program by reassigning it a new value.
  9. PHP is a dynamically-typed language, which means that the data type of a PHP variable can change automatically based on the value it is assigned. This is known as type juggling and can sometimes lead to unexpected results if not handled properly.
We value your feedback.
+1
0
+1
1
+1
0
+1
0
+1
0
+1
0
+1
0

Subscribe To Our Newsletter
Enter your email to receive a weekly round-up of our best posts. Learn more!
icon

Leave a Reply

Your email address will not be published. Required fields are marked *