PHP Variable

A variable like a pot in PHP. We can hold many data using a variable. Like a text string "Hello World" or Integer value like 500. We can use a variable in the whole script if we set variable value top in the script. We don't need to use value repeatedly if we use variable. Here are some rules to declare a variable.

1. In PHP variable must start with "$", otherwise, it'll not work.
2. Variables are case sensitive. $the_variable & $The_Variable is not same.
3. You must start a variable with letter or "_" (underscore). Any number isn't accepted starting in a variable.
4. PHP accepts alpha-numeric characters and underscores. a-z, A-Z, 0-9, or _ in a variable.
5. PHP doesn't accept space in a variable. example- '$helloworld', '$HelloWorld', '$Hello_World' is correct variable but '$hello world' isn't a correct variable.
Here are some example and discussion about PHP variable. Let's see how we write or declare a variable in the script.

<?php

$name = "Sajjad";

echo "Hello World!";

echo "<br/>";

echo "My name is ";

echo $name;

?>

output,
Hello World!
My name is Sajjad

PHP Varible Basic
Here is a simple math with variable,

<?php
$FirstNumber = 10;
$SecondNumber = 5;

echo $FirstNumber+$SecondNumber; //Summation Output 15
echo "<br/>";
echo $FirstNumber-$SecondNumber; //Subtraction Output 5
echo "<br/>";
echo $FirstNumber*$SecondNumber; //Multiplication Output 50
echo "<br/>";
echo $FirstNumber/$SecondNumber; //Division Output 2
?>
Output,
15
5
50
2

PHP Varible Math

You can join to many variables using (.) dot. Here is an example

<?php
$a = "Hello";
$b = "World! ";
$c = "My name is ";
$d = "Sajjad";

echo $a.$b.$c.$d; // Output Hello Wordl! My name is Sajjad
echo '<br/>';
echo $a.$b; // Output Hello Wordl!
?>
Output,
HelloWorld! My name is Sajjad
HelloWorld!

PHP Variable Join
If you set your variable value empty then PHP detect it default value 0 and count this variable value 0
example -

<?php
$number++;
echo $number;
?>
And,

<?php
$number = $number+1;
echo $number;
?>
both are executed 1 as their result.
that means PHP execute result with the default value.


Did you like this article? it will be appreciated if you share a coffee or burger with the author

Sent $5 to the author
Sent $10 to the author

Need Assistance?

I'm Sajjad Hossain, working on web application development since 2012. Do you need assistance on your project? or are you stuck with problems? I am available to help you.
If you want to contact with me ping me at -

WhatsApp
Skype


We use cookies on our website. To find out more about how and why they are used or opt-out, please read our Cookie Policy. By choosing "I Accept", you consent to our use of cookies. Cookie Policy
Top