AgerNic.com
WEB DEVELOPER SITE, HTML, CSS, PHP, SQL

PHP variable


<< Previous Page
PHP Syntax
Next Page >>
PHP echo - print

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:

<?php
$var = "text_here";
$var_1 = number_here;
?>
Note: Remember that PHP variable names are case-sensitive! $age and $AGE are two different variables

 

Creating (Declaring) PHP Variables example

In PHP variable can be declared as: $myvar_name = value;

Example

<?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
?>

Note: When you assign a text value to a variable, put quotes around the value, if you assign a number you do not ude quotes

 

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.

Example (this is HTML - PHP editor, change text on this window)
 <?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 used

echo "<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

Example (this is HTML - PHP editor, change text on this window)
<?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

Online Editor
ONLINE EDITOR

news templates


COLOR PICKER

news templates
This tool makes it easy to create, adjust, and experiment with custom colors for the web.


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


CSS HTML Layout
news templates
Find here examples of creative and unique website layouts.


Free CSS HTML Menu
news templates
Find here examples of creative and unique website CSS HTML menu.


Home
PHP Introduction
PHP Install
PHP Syntax
PHP Variables
PHP Echo and Print
PHP Data Types
PHP String Function
PHP Constants
PHP Operator Types
PHP If Else and Elseif
PHP Switch
PHP While Loops
PHP Loop For Do Foreach
PHP Array
Convert Array to String
PHP Function
PHP GET and POST
PHP Date/Time Functions
PHP Login
PHP Delete Element
PHP eregi_replace()
PHP mysql_query()
PHP Errors to Display
PHP Loop For Do Foreach

PHP forms
PHP Form Example
Online Editor
ONLINE EDITOR

news templates


COLOR PICKER

news templates
This tool makes it easy to create, adjust, and experiment with custom colors for the web.


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


CSS HTML Layout
news templates
Find here examples of creative and unique website layouts.


Free CSS HTML Menu
news templates
Find here examples of creative and unique website CSS HTML menu.


Upload Image, display, edit and delete i...
Login Form with Image background...
How to Create an Image with Transparent ...
Portfolio Gallery Website with filtering...
Simple pagination script PHP MySQLi...
Center Image in div...
Image Hover Overlay Fade...
Sticky image / element / div on scroll...
Responsive images...
Create rounded image in HTML CSS...
Add border around image...
Position Text Over an Image HTML CSS res...
Create a Slideshow Images Gallery...
Create a Sticky Sidebar...
Search bar using CSS HTML...
Shrink Navigation Menu on Scroll...
How to Create Accordion HTML Templates...
Dropdown menu in the navigation bar...
Responsive Top Navigation Bar with mobil...
Split horizontal navigation bar...