32,000+ SUBSCRIBERS CAN'T GO WRONG !!!

phpans
GET TOP ARTICLES, SNIPPETS, SCRIPTS IN YOUR INBOX FOR FREE!

Delivered by GET EMAIL UPDATES FeedBurner
We always respect your privacy and take protecting it seriously.
We won't Spam You

PHP Variable

, 0,
PHP Variable
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 "_" (under score). 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 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 is some example and discussion about PHP variable. Let's see how we write or declare a variable in 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
Don't Miss: PHP Operator


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 execute 1 as their result.
that means PHP exexute result with the defaulf value.

Download PDF
Is this article helpful for you?





captcha

  • Connect
  •       
Top
Create an free account and start learning & sharing! Sign Up