PHP Variables

What is Variables?

Variables are used to store information in temporary memory. A variable is assigned a value in one place and then be used throughout your program. A variable does not need to be declared before adding a value to it. PHP automatically converts the variable to the correct data type, depending on its value.

Variables in PHP

To make variable in PHP.

  • PHP variables must start with a $ sign.
  • Variables names can contain letters(a-z and A-Z), numbers(0-9), letters and numbers(a-9) and underscores(_).
  • Variable names cannot begin with numbers.
  • Variable names cannot contain spaces.
  • Variable is more than thirty-characteristic on the character.

Make variable in different correct ways :

<?php

$a;
$variable;
$B_long;
$variable_name;
$_variable;
$_variable_name;
$a_variable_name;
$variable_Number;
$myVariable;
$variable_1234;
$_1234;

?>

Variable in wrong way :

<?php

$1234;
$$1234;

?>

How to store value in Variable

The assignment (=) used to assign value to a variable. When you assign a text value to a variable, put quotes around the value. PHP variable can be creating as: $variable_name = value;

Declaring variables

<?php

$text = "Hello World!";
$number = 10;
$textandnumber = $text . $number;
$textandnumberwithspace = $text . " " . $number;

?>

Displaying variables value

<?php

$text = "Hello World!";
echo $text;
// Output: Hello World!
$number = 10;
echo $number;
// Output: 10
$textandnumber = $text . $number;
echo $textandnumber;
// Output: Hello World!10
$textandnumberwithspace = $text . " " . $number;
echo $textandnumberwithspace;
// Output: Hello World! 10

?>

Share This:

No comments:

Post a Comment

Advertiser
Counter
Flag Counter