Jump to content
xisto Community
KansukeKojima

Php Variable Basics Where would PHP programmers be without the variable? Learn all about t

Recommended Posts

<?php$var= "This is a variable";echo $var; ?>OutcomeThis is a variable
Using what we know about the echo function, we can see that we have created a variable, and then used the echo command to display it. This is fairly simple, and can have many uses, but there is much more too it!

<?php$var1 = "This is variable one";$var2 = "This is variable two";$varall = "$var1. $var2. You just combined three variables in one!";echo $varall;?>OutcomeThis is variable one. This is variable two. You just combined three variables in one!";
So what? We may as well have just put whatever was in $var1 and $var2 in $varall to begin with, right? In this case, yes. However, lets say that we want to have an affiliate section on our page. We could have all the information (buttons, links, description, etc.) in one variable, or each can have a seperate variable, allowing for easy editing. Another bonus is the ability to place things anywhere on a page without having to code it every time. Heres an example:
<?php$link1 = "link to wherever";$link2 = "different link";$content = "Bla BLA Bla... Bla bla bla $link1... please go here! $link2... if you want then go here to $link1...";echo $content;?>
Understand what this would enable you to do? Instead of writing out a link every time you need it, you can simply type a nice short variable! This is far more efficient, and easy to read. There is many other things that you can do with this (example: placing all your pages in one PHP file... yeah... you can do that), but thats all for now!

Share this post


Link to post
Share on other sites

I always thought whith php.. here let me remake the lost code block in the way i would have done it.

<?php$linkOne = "link one";$linkTwo = "Link two";$content = "Bla bla bla.. bla bla bla " . $linkOne . " ... please go here!" . $linkTwo . ".. if you want then go here to " . $linkOne . "...;?>

well i really don't know, i just started learning php yesterday, and i havn't finished... and i recall it being somehting like that (may not be exactly)
Edited by Tramposch (see edit history)

Share this post


Link to post
Share on other sites

Or better still is this approach to have the php variable include the html code for the link:

<?php$linkOne = '<a href="http://trap17.com; title="my favourite place" > link one </a> ';$content = 'Bla bla bla.. bla bla bla ' . $linkOne;echo "$content";?>

Share this post


Link to post
Share on other sites

You said "Learn All About It" but you have not covered a lot of things - in fact, you have just covered the string concatenation operator :) So you will need arithmatic operators (+, -, *, /, %) and different variable types (int and double). The modulus operator "%" is special because you do not learn it at primary school...

Share this post


Link to post
Share on other sites

hmmm... it was suppost to say all about the basics.... guess it got cut off.......??

Share this post


Link to post
Share on other sites

Another approach to jlhaslip's code would be:

<?php$linkOne = '<a href=\'http://trap17.com; title=\'my favourite place\'> link one </a> ';$content = 'Bla bla bla.. bla bla bla ' . $linkOne;echo "$content";?>
This allows you to use more than one ' or " in a variable.

Share this post


Link to post
Share on other sites

Another approach to jlhaslip's code would be:

<?php$linkOne = '<a href=\'http://trap17.com; title=\'my favourite place\'> link one </a> ';$content = 'Bla bla bla.. bla bla bla ' . $linkOne;echo "$content";?>
This allows you to use more than one ' or " in a variable.

Single quotes are used to display character as it is. If you are using single quotes you don't need to, and you can't escape special characters.

This should write
$linkOne = '<a href="http://forums.xisto.com/; title="my favourite place"> link one </a> ';
Edited by pop (see edit history)

Share this post


Link to post
Share on other sites

Wow, you guys have successfully confused me even more :)
Well.. are they all an ok way to do it? or is one wrong lol

I haven't tested the other methods, but the distinction seems to be about the use of single or double quotes and the use of escape characters in the echo command.
I will summarize what i know and hope that it becomes clear to you.
When you echo a line using double quotes, the line gets parsed by the php parsing engine, and special characters may need to be escaped so the parser ignores them as command characters.
Using single quotes, the same characters do not require escaping since single quoted echo lines do not get parsed.
There are often several methods available for the echo statement. If you have a variable to echo, and want it parsed, use double quotes.

Otherwise, single quotes are faster because the content is not parsed.
Still confused? Me too... try this code:
<?php$var1 = "variable one for printing here";echo $var1 . '<br />';echo '$var1' . ' with single quotes<br />';echo "$var1" . '<br />';echo $var1 . " with double quotes<br />";?>

Share this post


Link to post
Share on other sites

I always find out if its the wrong way to do something when I create the page, there is an error, and then I have to spend a good hour figuring out whats wrong :).....It really helps to re-inforce how to properly code when you waste so much time... :{

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×
×
  • Create New...

Important Information

Terms of Use | Privacy Policy | Guidelines | We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.