• Skip to main content
  • Skip to primary sidebar
  • Skip to footer

Matt Doyle | Elated Communications

Web and WordPress Development

  • About Me
  • Blog
    • Design & Multimedia
      • Photoshop
      • Paint Shop Pro
      • Video & Audio
    • Online Marketing
      • E-Commerce
      • Social Media
    • Running a Website
      • WordPress
      • Apache
      • UNIX and Linux
      • Using FTP
    • Web Development
      • HTML
      • CSS
      • JavaScript
      • PHP
      • Perl and CGI Scripting
  • Portfolio
  • Contact Me
  • Hire Me
Home / Blog / Web Development / PHP / PHP Variables

PHP Variables

18 May 2009 / 4 Comments

Like most programming languages, PHP lets you create variables in your scripts. A variable is a storage container that holds a value. This value can change as the script runs. You can:

  • Assign any value you like to a variable
  • Access the value stored in a variable, and
  • Change a variable’s value at any time.

Variables are useful because they let you write flexible scripts. For example, a script that can only add 3 and 4 together isn’t very useful. A script that can add any two values together, though, is much more flexible.

Creating a variable

To create a new variable in PHP, you can just write the variable’s name:


$myVariable;

Notice the dollar ($) symbol before the variable name. All PHP variables have a dollar symbol in front.

This is known as declaring a variable. It’s also a good idea to give the variable an initial value at the time you declare it — this is known as initializing the variable:


$myVariable = 23;
If you don’t initialize a new variable then it takes on a value of null.

Changing a variable’s value

You’ve just seen how to assign a value to a variable: you simply write the variable name, followed by an equals sign, followed by the value you want to assign.

To change a variable’s value, simply assign the new value:


$myVariable = 23;
$myVariable = 45;
$myVariable = "hello";

The first line of code creates a new variable with a numeric value of 23, while the second line changes the variable’s value to 45. The third line changes the value again — this time to a string of text, “hello”.

PHP is a loosely-typed language, which means you can change the type of data that a variable holds whenever you like. In the above example, $myVariable starts off holding a number, and finishes by holding a string of text.

Using a variable’s value

To use the value of a variable in your script, simply write the variable’s name. For example, to display the value of $myVariable you’d use:


echo $myVariable;

To add the values of two variables $x and $y together and display the result, you could write:


echo $x + $y;

Variable names in PHP

Before leaving the topic of PHP variables, it’s worth taking a look at how to name variables. All PHP variable names have to follow these rules:

  • They start with a $ (dollar) symbol.
  • The first character after the dollar must be either a letter or a _ (underscore) symbol.
  • The other characters may be letters, numbers, and/or underscores.

Variable names can be as long as you like, but it’s a good idea to keep them relatively short (otherwise they can get quite unwieldy). It’s also good to use meaningful variable names — for example, $numWidgets is much more useful than $nw.

PHP variable names are case-sensitive, which means that $myVariable and $MyVariable refer to different variables.

Now that you know how PHP variables work, you’re well on your way to writing useful PHP scripts!

Filed Under: PHP Tagged With: assigning values, changing values, creating variables, php basics, php tutorial, php variable names, PHP variables, reading values, using variables

Reader Interactions

Comments

  1. Johno says

    6 September 2009 at 12:04 am

    Nice article, thanks. Is there any way to find out the type of data that’s stored in a variable?

    Reply
  2. chrishirst says

    6 September 2009 at 12:43 am

    http://www.php.net/manual/en/function.gettype.php

    Reply
  3. matt says

    6 September 2009 at 2:19 am

    Also if you want to test for specific types of data you can also use the is_* functions, described in “Finding out the data type of a value”: http://www.elated.com/articles/php-data-types/

    Reply
  4. Johno says

    6 September 2009 at 3:06 am

    Perfect – thanks for your help chrishirst & matt 😀

    Reply

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

To include a block of code in your comment, surround it with <pre> ... </pre> tags. You can include smaller code snippets inside some normal text by surrounding them with <code> ... </code> tags.

Allowed tags in comments: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong> <pre> .

Primary Sidebar

Hire Matt!

Matt Doyle headshot

Need a little help with your website? I have over 20 years of web development experience under my belt. Let’s chat!

Matt Doyle - Codeable Expert Certificate

Hire Me Today

Call Me: +61 2 8006 0622

Stay in Touch!

Subscribe to get a quick email whenever I add new articles, free goodies, or special offers. I won’t spam you.

Subscribe

Recent Posts

  • Make a Rotatable 3D Product Boxshot with Three.js
  • Speed Up Your WordPress Website: 11 Simple Steps to a Faster Site
  • Reboot!
  • Wordfence Tutorial: How to Keep Your WordPress Site Safe from Hackers
  • How to Make Awesome-Looking Images for Your Website

Footer

Contact Matt

  • Email Me
  • Call Me: +61 2 8006 0622

Follow Matt

  • E-mail
  • Facebook
  • GitHub
  • LinkedIn
  • Twitter

Copyright © 1996-2023 Elated Communications. All rights reserved.
Affiliate Disclaimer | Privacy Policy | Terms of Use | Service T&C | Credits