Web Development

How to Setup and use Printing Variables in PHP

How to Setup and use Printing Variables

A variable, is something which can hold a dynamic value.  A variable is distinguished from other things because it starts with a dollar sign.  For example, "$name" could be a variable which would most likely hold the name of something, someone, or someplace.  Variable declaration requires the use of the "assignment operator" ("=") which will assign a value to our variable.

What you assign to a variable also requires a little thought.  If you’re assigning a number to a variable, then quotes are not required.  If you’re assigning a string to a variable, then quotes are required.  The two basic types a variable can be are "string" and "number" and PHP understands which one you want with the presence/absence of quotes.

$name = ‘Parham’; //this will assign ‘Parham’ to the variable $name, note the quotes

$number = 12; //this will assign 12 to the variable $number, note the absence of quotes

$othernumber = ’12′; //this will assign ’12′ to the variable $number, not the presence of quotes, the variable is now interpreted as a string, not as a number

If you literally want to use quotes in your strings then they will have to be "escaped" so PHP will not get confused:

About the author

Written by Darren W..

Darren Hedlund is a freelance Web developer, writer, and data analyst. Darren has a degree in Computer Information Science and has spent the last 15 years developing application and environments from hand held, windows, web, virtual science, gaming, artificial intelligence and graphics design.

Darren's coding knowledge ranges from C+, Visual Basic, .NET, PHP, JSP, REXX, KIXX, and many others. His graphical and environmental knowledge stems in Macromedia Flash, 3D studio Max, Curious Labs Poser, Adobe Photoshop, and many others. Darren works in many platforms ranging from database, visual design, and, system development.

If you found this post useful you may also want to check these out:

  1. How to Setup Variable Scope and Global Declarations
  2. Use Stack Variables Whenever Possible
  3. Develop Rock-Solid Code In PHP: Use Variables Effectively, Part 2
  4. PHP Operators
  5. Use Synchronized or Volatile when Accessing Shared Variables
  6. Using Control Structures and Foreach Loops in PHP