What is PHP variable and what is its importance in PHP?

· Dev & Design
Authors

As with any other programming languages, PHP allows to define variables. In PHP there
are many variable types,but the most common among them is a String variable.


String Variable

A String variable holds text and numbers. All strings begin with a symbol “$”.
To assign some text to the string you can use the following code:

$welcome_text = “Hello and welcome to VYOM Technosoft”;

Strings are case sensetive so the string variable $Welcome_Text is not the same as $welcome_text
A string name can contain letters, underscores and numbers. but it should not begin with a number
or underscore. When assigning numbers to the string variables you need not include the quotes so:

$user_id = 987

would be allowed.


How to display the Variable.

We use exactly the same code to display a variable on the screen, as we used to display
the text. But slightly different form. The following code would display the
welcome text:

<?
$welcome_text = “Hello and welcome to VYOM Technosoft”;
print($welcome_text);
?>

As you can see, the only major difference is that you need not put the
quotation marks if you are printing a variable.


How to format the Text?

Output from your PHP programs is quite boring unfortunately. All the content is just
output in the browser’s default font. It is easy, to format your text using
HTML. This is because, as we know that the PHP is a server side scripting language,
code is been executed before the page is sent to browser. This means, only the resulting
information from script is sent, so in the above example, the browser would just sent the text:

Hello and welcome to VYOM Technosoft

This means, that you can include standard HTML markup in your strings and scripts.
The problem with this is that many HTML tags require the ” symbol. You may notice that
this symbol will clash with the quotation marks used to print the text. To overcome this confusion
you must tell the script which quotes to be used (those at the beginning and end of the
output) and which to be ignored (those of the HTML code).


How to solve this problem?

To illustrate this let us consider the example, Change the font of the text to the
Arial font in red. The code for this is as follows.

<font face=”Arial” color=”#FF0000″>
</font>

As you can see this code contains 4 quotation marks so can confuse the script.
To solve this you must add a backslash before each quotation mark to make the PHP
script ignore it. Then the code would look like.

<font face=\”Arial\” color=\”#FF0000\”>
</font>

Now you can include this in your print statement,which will make the browser display it.

print(“<font face=\”Arial\” color\”#FF0000\”>Hello and welcome to
VYOM Technosoft</font>”);

Output:Hello and welcome to VYOM Technosoft

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s