DEV Community

Cover image for UNDERSTANDING VARIABLES AS A BEGINNER IN PHP
Oswin Ayonoadu
Oswin Ayonoadu

Posted on

UNDERSTANDING VARIABLES AS A BEGINNER IN PHP

Introduction

​​What is a Variable?
​​
​​Variables are containers or placeholders which are used to store data, and that data can be used by assigning a name to the variable. Variables are very useful and powerful, it is used in all kinds of programming languages.
​​
​​VARIABLES IN PHP
​​
​​In PHP, a variable is declared with the dollar sign $, followed by the name of the variable:
​​
​​Example:

​​<?php
​​$name = “Oswin”;
​​$number = 100;
​​echo $name;
​​echo $number
​​?>
Enter fullscreen mode Exit fullscreen mode

​​

​​Factors to consider before defining a variable

​​

  • The symbol = is an assignment operator used to assign the variable name or value.

  • Numbers In variables do not have or need quotes “” around them, quotes are used to identify strings. This is how PHP knows that it is a number.

  • The function echo is used to execute the value by using the variable name.
    echo $name;
    ​​echo $number;

  • You can choose to write variables however you wish, could be in upper cases or lowercases.
    ​​$Number
    ​​$NUMBER

​​Both variables above are acceptable but are different from each other. A value assigned to one cannot to called out by the other, why? Because variables are case sensitive.
​​
​​For best results it is advisable to use all lowercase. If variable contains two words, then you can separate words with either an underscore _ or an uppercase.
​​ $numerList = 100
​​ $number_list = 100
​​
​​
​​

Rules for declaring PHP variable:

  • A variable must start with a dollar sign, $ followed by the variable name.

  • It contains only alpha-numeric characters and underscore.

  • A variable cannot start with a number or special symbols, it must start with an alphabet or underscore _

  • A variable name cannot contain any space.

  • PHP variables are case sensitive.
    ​​

CONCATENATION IN PHP

Concatenation is basically a series of interconnected things, joining things together.
​​
In PHP you can add two or more different variable values together using concatenation.
​​It is done with the used of a dot (.)
​​

​​<?php
​​$name = “Edwin”;
​​$number = 100;
​​echo $name . $number;
​​?>
​​ 
​​RESULT :  Edwin100
​​
Enter fullscreen mode Exit fullscreen mode

​​note: To create a space between two values, you use the quote symbol “ “
​​
​​

echo $name . “ “ . $number
​​
​​RESULT : Edwin 100
​​
Enter fullscreen mode Exit fullscreen mode

​​Things that can be assigned to variables in PHP

​​

  • We can assign html tags to variables ​​
$name = “<h1> HELLO </h1>”;
Enter fullscreen mode Exit fullscreen mode
  • We can assign images to variables ​​
  • We can assign many different data into a variable.

  • Keep in mind that you can only assign one value at a time.
    ​​

​​Declaring a text, integer, and float

​​
​​Let's look at a PHP variable example that stores string, integer, and float values.
​​
​​Example
​​

<?php  
​​$str="hello there";  
​​$x=56;  
​​$y=56.6;  
​​echo "string is: $str <br/>";  
​​echo "integer is: $x <br/>";  
​​echo "float is: $y <br/>";  
​​?>  
Enter fullscreen mode Exit fullscreen mode

​​
​​
Output

​​string is: hello there
​​integer is: 56
​​float is: 56.6 
​​
Enter fullscreen mode Exit fullscreen mode

​​

​​Case Sensitivity

​​Variable names in PHP are case-sensitive. The term "color" is so distinct from the terms Color, COLOR, COLor, etc.
​​
​​Example
​​

<?php  
​​$color="green";  
​​echo "My pen is " . $color . "<br>";  
​​echo "My house is " . $COLOR . "<br>";  
​​echo "My boat is " . $coLOR . "<br>";  
​​?>  
Enter fullscreen mode Exit fullscreen mode

​​
​​
​​Output
​​

My pen is green
​​Notice: Undefined variable: COLOR in C:\wamp\www\variable.php on line 4
​​My house is 
​​Notice: Undefined variable: coLOR in C:\wamp\www\variable.php on line 5
​​My boat is 
Enter fullscreen mode Exit fullscreen mode

​​
​​

​​Conclusion

​​Depending on its value, PHP automatically assigns a data type to the variable. Since the data types are not strictly defined, it is possible to add a string to an integer without running into problems.
​​

Top comments (0)