Adam Hassan
Jan 7, 2020

In this tutorial, I am going to explain what meant by php variables and are their types with programming examples for beginners. Php variables are unknown amounts that could be called by any name starting with "$" dollar sign. The php allows to set a value to the variable and automatically detects the type of set value.

What are Php Variables and Types ?

For example, the username and password in a sign in form are variables and are set by the user.

How to declare php variable with and without value

In general, php allow to declare variable without value as below:

<?php
$name1;
?>

This means it contains nothing or a value of null, also it is referred to as a global variable across files as long as it is out of a php function or class code blocks, where php functions or classes will be explained in a later tutorial.

Php variables types and each variable type declaration

There are several php variables types and each variable type is declared as follow:

How to properly name a php variable

It is important to know that a php variable could be only named with a minimum of one letter character after "$" sign and not a number character like $4name but can be $n4ame or $na4me and so on.

Ways to display a php variable

Php variable could be displayed using "echo" keyword for all types containing single value and "print_r" keywords for array type containing series of values. As an example:

<?php
$name1 = 'a string may be abc or 123 or !@#';
echo $name1;
$name2 = [123,-1.23,"abc123"];
print_r($name2);
?>

How to display a php variable in string

Note that a php variable could displayed in string between only double quotes and not between single quote to complete any value. As an example:

<?php
$name1 = 'a string may be abc ';
$name2 = 'or 123 or !@#';
echo "$name1 $name2";
?>

This will show in browser:

a string may be abc or 123 or !@#

How to set and alter a value to a php variable

The variable could be set several times and maintain only the last set value. As an example:

<?php
$name1 = 'a string may be abc ';
$name1 = 'or 123 or !@#';
echo $name1;
?>

This will show in browser:

or 123 or !@#
Thank you for reading the above article. Please let us know your comments below.
Subscribe to follow the next tutorial : Php Arithmetic and Comparison Operators >>>

References:

  • Practical Experience
About the author

Adam Hassan

Adam is a freelance web designer, recreational software developer, author and CEO of infoapper.com . Also, has passion to learn and transfer the know-how of every valuable thing.
Views: 80
0 Comment Guest
Recommend 0
Sort by Newest
Be the first to say something...

Ask Community

Ask questions and Share knowledge with Community

Find below recent posts for automation solutions with questions and answers by community. You can search in past threads or post new question about your assignment with detailed description, and always could mark your question as request. Sharing knowledge are highly appreciated by answering on others questions, and in return awards will be decided.

#
Recent Questions
*
i need to know how to convert 723,413,000 sq mm into mm2.
96
1
0
*
How to convert kg/m3 to kn/m3
64
1
0

  View More

× Close
Results: