PHP Variables

Submitted by: 
Language: 
Visitors have accessed this post 13334 times.

A variable is used to store any value that you like.

You can reuse variable in any block of code in your PHP script.

PHP variables must start with a dollar ($) sign.

PHP Variable Syntax

$variable_name = value;

Below is an example of how to use variables in PHP:

  1. <?php
  2. $firstname = "John";
  3. $lastname = "Smith";
  4. echo $firstname . ' ' . $lastname;
  5. echo "<br />";
  6. $x = 55;
  7. echo $x
  8. ?>

The above code will output the following on your web browser:

John Smith
55

As you can see above variables are not declared first before you can use it. PHP is a loosely type language and all you need is to declared the variable properly.

PHP variables must start with letters and underscore only. The same as other language, PHP variable must not contain spaces.


Comments

its a good site to learn and practice

Add new comment

Filtered HTML

  • Web page addresses and e-mail addresses turn into links automatically.
  • You may insert videos with [video:URL]
  • Allowed HTML tags: <a> <em> <strong> <cite> <blockquote> <code> <ul> <ol> <li> <dl> <dt> <dd> <table> <tr> <td> <th> <img> <h1> <h2> <h3> <iframe> [video]
  • You can enable syntax highlighting of source code with the following tags: <code>, <blockcode>, <asp>, <c>, <cpp>, <csharp>, <css>, <html4strict>, <java>, <javascript>, <mysql>, <php>, <python>, <sql>, <vb>, <vbnet>. The supported tag styles are: <foo>, [foo].
  • Lines and paragraphs break automatically.

Plain text

  • No HTML tags allowed.
  • Lines and paragraphs break automatically.
CAPTCHA
This question is for testing whether or not you are a human visitor and to prevent automated spam submissions.