PHP variable
PHP Tutorial » PHP variable
What is a PHP variable?
Variables are "containers" for storing data, like string of text, numbers, etc
What to know about variables:
In PHP, a variable does not need to be declared before adding a value.
After declaring a variable, it can be reused in full code.
Always start with "$" without quotes, followed by the variable name, then an "equal sign" (=), and followed the text included in quotation marks with ("), followed by ( ;) as a command end.
If we assign numbers to the variable, we do not have to add quotes
Syntax:
$var = "text_here";
$var_1 = number_here;
?>
Creating (Declaring) PHP Variables example
In PHP variable can be declared as: $myvar_name = value;
<?php
// Declaring variables as text
$txt1 = "Hi, this is my variable!";
$txt2 = "Hi, this is another text variable!";// Declaring variables as numbers
$number1 = 7;
$number2 = 19;
$number3 = 247;
// Displaying variables value
echo $txt1; echo "<br />"; // Output: Hi, this is my variable!
echo $txt2; echo "<br />"; // Output: Hi, this is another text variable!echo $number1; echo "<br />"; // Output: 7
echo $number2; echo "<br />"; // Output: 19
echo $number3; echo "<br />"; // Output: 247
?>
Related subjects:
PHP syntax
What is an array
PHP variabl
Array to comma separated
Tags: php variable: in string, type, variables, scope, naming conventios, number of parameters, in javascript, declaration, inside string,
What is a PHP variable?
How do you declare a PHP variable?
What is PHP and $$ variables?
How do you call a variable in PHP?
PHP Variable in String
A method to interpolate a PHP variable with string literal by assigning the string literal to a variable.
<?php$txt1 = "sweet";
echo "{$txt1}ened";echo "<br /> ---------------------------- <br />";
$prefix1 = "Comfort";
$suffix1 = "able";
echo "{$prefix1}{$suffix1}";echo "<br /> ---------------------------- <br />";
$my_number = '56789';
// This works:
echo "qer{$my_number}rty <br />"; // qer56789rty, using braces
echo "qer" . $my_number . "rty"; // qwe12345rty, concatenation usedecho "<br /> ++++++++++++++++++++++++++++++++++++ <br />";
// Does not work:
?>
echo 'qwe{$my_number}rty <br />'; // qwe{$a}rty, single quotes are not parsed
echo "qwe$my_numberrty"; // qwe, because $a became $arty, which is undefined
PHP Concatenation
In the next example Variable($my_car) hold value="Ford Mondeo". Now we concatenate variable with string. pass this variable($my_car) inside echo statement. To concatenate this with a string("is riding") use dot(.) between variable name and string. The output will be displayed : Ford Mondeois riding
<?php$my_Car = "Ford Mondeo";
echo $my_Car." is riding";
?>
php variable in string, type, variables, scope, naming conventios, number of parameters, in javascript, declaration, inside string,
What is a PHP variable?
How do you declare a PHP variable?
What is PHP and $$ variables?
How do you call a variable in PHP?
PHP variable in string, variables declaration - PHP tutorial - php tutorial
This tool makes it easy to create, adjust, and experiment with custom colors for the web.

Magnews2 is a modern and creative free magazine and news website template that will help you kick off your online project in style.

Find here examples of creative and unique website layouts.

Find here examples of creative and unique website CSS HTML menu.