Welcome Guest ( Log In | Register)



 
Reply to this topicStart new topic
> Php Variable Basics, Where would PHP programmers be without the variable? Learn all about t
KansukeKojima
post Nov 24 2007, 09:17 AM
Post #1


Privileged Member
*********

Group: [HOSTED]
Posts: 528
Joined: 13-October 06
From: Alberta, Canada
Member No.: 31,584



CODE
<?php
$var= "This is a variable";
echo $var;
?>
Outcome
This 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!

CODE
<?php
$var1 = "This is variable one";
$var2 = "This is variable two";
$varall = "$var1. $var2. You just combined three variables in one!";
echo $varall;
?>
Outcome
This 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:
CODE
<?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!
Go to the top of the page
 
+Quote Post
Tramposch
post Nov 24 2007, 01:51 PM
Post #2


Super Member
*********

Group: Members
Posts: 219
Joined: 27-May 07
From: SOME WHERE
Member No.: 43,760



I always thought whith php.. here let me remake the lost code block in the way i would have done it.
CODE
<?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)

This post has been edited by Tramposch: Nov 24 2007, 01:51 PM
Go to the top of the page
 
+Quote Post
jlhaslip
post Nov 24 2007, 02:49 PM
Post #3


A computer once beat me at chess, but it was no match for me at kick boxing.
Group Icon

Group: [MODERATOR]
Posts: 3,968
Joined: 24-July 05
From: In Trouble Again... still?
Member No.: 9,787
Spam Patrol



Or better still is this approach to have the php variable include the html code for the link:
CODE
<?php
$linkOne = '<a href="http://www.trap17.com" title="my favourite place" > link one </a> ';
$content = 'Bla bla bla.. bla bla bla ' . $linkOne;
echo "$content";
?>
Go to the top of the page
 
+Quote Post
laexter
post Nov 24 2007, 04:38 PM
Post #4


Newbie [Level 1]
*

Group: Members
Posts: 18
Joined: 3-November 07
From: Jakarta, Indonesia
Member No.: 52,426



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 smile.gif 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...
Go to the top of the page
 
+Quote Post
KansukeKojima
post Nov 24 2007, 05:35 PM
Post #5


Privileged Member
*********

Group: [HOSTED]
Posts: 528
Joined: 13-October 06
From: Alberta, Canada
Member No.: 31,584



hmmm... it was suppost to say all about the basics.... guess it got cut off.......??
Go to the top of the page
 
+Quote Post
GodFather
post Nov 24 2007, 06:37 PM
Post #6


Newbie [Level 1]
*

Group: Members
Posts: 15
Joined: 24-November 07
Member No.: 53,658



Another approach to jlhaslip's code would be:
CODE
<?php
$linkOne = '<a href=\'http://www.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.
Go to the top of the page
 
+Quote Post
pop
post Nov 25 2007, 04:47 AM
Post #7


Advanced Member
*******

Group: Members
Posts: 102
Joined: 13-October 07
Member No.: 51,530



QUOTE(GodFather @ Nov 24 2007, 07:37 PM) *
Another approach to jlhaslip's code would be:
CODE
<?php
$linkOne = '<a href=\'http://www.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://www.trap17.com" title="my favourite place"> link one </a> ';

This post has been edited by pop: Nov 25 2007, 04:48 AM
Go to the top of the page
 
+Quote Post
Tramposch
post Nov 25 2007, 02:16 PM
Post #8


Super Member
*********

Group: Members
Posts: 219
Joined: 27-May 07
From: SOME WHERE
Member No.: 43,760



Wow, you guys have successfully confused me even more smile.gif

Well.. are they all an ok way to do it? or is one wrong lol
Go to the top of the page
 
+Quote Post
jlhaslip
post Nov 25 2007, 04:16 PM
Post #9


A computer once beat me at chess, but it was no match for me at kick boxing.
Group Icon

Group: [MODERATOR]
Posts: 3,968
Joined: 24-July 05
From: In Trouble Again... still?
Member No.: 9,787
Spam Patrol



QUOTE(Tramposch @ Nov 25 2007, 07:16 AM) *
Wow, you guys have successfully confused me even more smile.gif

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:
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 />";

?>