So what is a variable you may ask? A variable allows you to store values and expressions. What does that mean exactly?
Let’s imagine you are writing a script for a client with some certain HTML that appears numerous times throughout your script. Now you can simply type out this HTML every single time or you can save yourself some time by storing the HTML into a variable.
Before I go any further you should understand the rules for a variable.
Rules for PHP variables:
- A variable starts with the $ sign, followed by the name of the variable
- A variable name must begin with a letter or the underscore character
- A variable name can only contain alpha-numeric characters and underscores (A-z, 0-9, and _ )
- A variable name should not contain spaces
- Variable names are case sensitive ($y and $Y are two different variables)
Now that you understand the structure of a variable, let’s put it to use.
Let’s imagine that this is the HTML code that we will use over and over again.
<div class="form-container">
<fieldset>
<ul>
<li class="name-field">
<span class="your-name">
<input type="text" name="name" value="" size="40" class="text-input" placeholder="Name (Required)" />
</span>
</li>
<li class="email-field">
<span class="your-email">
<input type="email" name="email" value="" size="40" class="text-input" placeholder="Email (Required)" />
</span>
</li>
<li class="subject-field">
<span class="your-subject">
<input type="text" name="subject" value="" size="40" class="text-input" placeholder="Subject" />
</span>
</li>
<li class="text-area-row">
<span class="your-message">
<textarea name="message" cols="40" rows="10" class="text-area"></textarea>
</span>
</li>
<li class="button-row">
<input type="submit" value="Submit" class="btn-submit" />
</li>
</ul>
</fieldset>
</div>
As you can see this would require a lot of typing if we needed to type this same code out numerous times in our script. Of course, there are many different ways we can do this but for the purposes of learning, we will assume that we need to contain this form into a variable.
One thing to notice in our HTML is the quote tags. This is extremely important when deciding how to contain the information in our variable. A variable can be defined by using either double quotes, single quotes, and in some cases no quotes at all.
If we were to use the double quote method when storing our info into the variable then we must escape each double quote in our string. Let’s take a look at this example:
$submitbtn = "<input type="submit" value="Submit" class="btn-submit" />";
Why do you think this would be incorrect? When PHP encounters the opening quote tag it will search for the closing quote tag. In this situation, PHP will assume it should store the above string like the below snippet which would cause an error.
<input type=
In order to prevent this, you would need to escape the double quotes with a leading backslash in our string. So the correct way to do this would be to do the following.
$submitbtn = "<input type=\"submit\" value=\"Submit\" class=\"btn-submit\" />";
The backslash essentially tells PHP to ignore the following character to prevent PHP from assuming the double quote should be the end of our string. However, In a situation that we face with our current HTML, which contains so many double quotes, it would simply be much easier to use single quotes to wrap our string. Consider the following:
$submitbtn = '<div class="form-container">
<fieldset>
<ul>
<li class="name-field">
<span class="your-name">
<input type="text" name="name" value="" size="40" class="text-input" placeholder="Name (Required)" />
</span>
</li>
<li class="email-field">
<span class="your-email">
<input type="email" name="email" value="" size="40" class="text-input" placeholder="Email (Required)" />
</span>
</li>
<li class="subject-field">
<span class="your-subject">
<input type="text" name="subject" value="" size="40" class="text-input" placeholder="Subject" />
</span>
</li>
<li class="text-area-row">
<span class="your-message">
<textarea name="message" cols="40" rows="10" class="text-area"></textarea>
</span>
</li>
<li class="button-row">
<input type="submit" value="Submit" class="btn-submit" />
</li>
</ul>
</fieldset>
</div>';
Now that beats using a thousand backslashes any day right? Of course it does!
Now that we have set our variable to contain our form HTML, we can simply echo it out onto our page by doing the following.
<?php
echo $submitbtn;
?>
That’s it! I hope you found this post to be useful and fun to learn!