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

free web hosting
Open Discussion > CONTRIBUTE > Tutorials

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

KansukeKojima
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!

 

 

 


Reply

Tramposch
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)

Reply

jlhaslip
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";
?>

Reply

laexter
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...

Reply

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

Reply

GodFather
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.

Reply

pop
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> ';

 

 

 


Reply

Tramposch
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

Reply

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

?>

Reply

KansukeKojima
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 tongue.gif.....

It really helps to re-inforce how to properly code when you waste so much time... :{

Reply



Got an Opinion! Express your Views! (no registration):-
Add your Reply/ Opinion/ Views/ Comments/ Suggestion/ Questions/ Queries etc.
Posts with decent grammar & English will be accepted and please refrain from profanities.
For asking a Question, We recommend you to sign-up (for free) so that you can track the topic easily.

Nature of your Post*: Opinion/ Reply/ Comments
Question/Query
Feedback to us.
       
Name   Email
Title/Question*

(Maximum characters: 10,000)
You have characters left.

Similar Topics

Keywords : php, variable, basics, php, programmers, variable, learn, t

  1. Css Basics No.2
    More basics of css (0)
  2. Want-to-start Html Tutorials
    An HTML tutorial that covers the basics (2)
    NOTE: Don't use a rich text editor* for writing HTML code! Use Notepad in Windows,
    SimpleText in Mac or TextEdit in OSX, but you must set the following preferences for the HTML code
    to work! In the Format menu, select Plain Text. Open the preferences window from the Text
    Edit menu, then select "Ignore rich text commands in HTML files." Start Creating Your Own HTML
    File You can either use HTM or HTML file extensions. For more information, visit:
    http://filext.com/file-extension/htm for HTM or http://filext.com/file-extension/html for HT....
  3. Css Basics
    Just for those who know nothing about css yet. (6)
    A tutorial on some of the basics of css.... I wrote if for my site, may as well post it here too
    /tongue.gif" style="vertical-align:middle" emoid=":P" border="0" alt="tongue.gif" /> CSS Basics
    Description For those of you that don't know, CSS stands for Cascading Style Sheets. The whole
    point of it is to easily edit all of the pages on your website, so you don't have to edit each
    on individually. In this tutorial, you will learn some of the basics. Try It Out Css Syntax has
    three parts to it. The object (where you want to edit), the property (what part ....
  4. Php Echo
    Learn what the echo can do for you. Includes basic echo and variable p (10)
    PHP Echo Description The echo() command has one purpose, displaying whatever you put in between the
    brackets. Learn to use it to your advantage. Try It Out There is your echo command. For now,
    it does nothing. It has no value to display. Outcome This is my text. Now this echo command
    has a value to display. Whatever you replace "This is my text." with will be displayed. Alright,
    lets move onto the next step. This time, we will include a variable in the echo command.
    $var = "This is a variable"; echo "$var. Congratualations, you just parsed a....
  5. How To Play The Guitar And Basics For Any Instrument
    introductory musical instruction focused on the guitar (0)
    So, you've had your guitar for years and it just sits around collecting dust. But you don't
    have the money to get lessons and you don't think you can play anyway. Well, most likely you
    can play, all you need is practice and little tolerance for pain in the first two weeks. Pain? Why
    pain? Pain because you must condition your fair fingertips for the constant bombardment of pressure
    from metal strings. (Nylon strings are a bit easier and I like them, but it is best to practice on
    the worst strings possible...accoustic, high action and thick gauge so that ....
  6. Learning Vb
    Part 1 -- The Basics (5)
    NOTE: This guide assumes you know how to operate your compiler, meaning create objects and forms.
    This guild will only teach you the scripting. Almost all program users and game players alike want
    to create their own software, whether it being a massive game, an easy-to-use calendar, or maybe
    even a hack. However, they must master, or at least know much about a programming language. If
    you've ever seen a Source Code , a text code written by a programmer and execute tasks by a
    computer, you'll know how much time and effort it takes to learn these nevertheless ....
  7. Php - The Basics
    Learn the basics of PHP (44)
    I should point out that I originally put this tutorial together for another site, but decided
    against it and to place it here instead (because I love you guys... ~sniffle, wipe a tear away~).
    This tutorial should help get you started if you are just setting out into the big bad world of eb
    development. I have been coding in PHP for a little over a year now, and have worked on several
    large projects in the language - but whilst I certainly don't know everything about it, I would
    like to share some of the basics that hopefully will give you a bit of a kick start. ---....
  8. Assembly Tutorial Part I
    The basics through "Hello world" (3)
    Okay, because assembly is an awesome language that is being viewed as archaic in the face of high
    level languages, I'm writing this series of tutorials. Note: This tutorial assumes a working
    knowledge of binary and hex. If you don't have a working knowledge of these number systems, and
    you are interested in learning assembler, let me know and I'll write a brief introduction to
    binary. Currently, until I can do some testing, this tutorial also only works for Windows and
    MS-DOS computers. If anyone with a Linux box is willing to test a routine for me, pleas....

    1. Looking for php, variable, basics, php, programmers, variable, learn, t

*RANDOM STUFF*





*SIMILAR VIDEOS*
Searching Video's for php, variable, basics, php, programmers, variable, learn, t

*MORE FROM TRAP17.COM*
advertisement



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



 

 

 

 

ADD REPLY / Got an Opinion! a humble request :-) RAPID SEARCH! Free Hosting [X]
Express your Opinions, Thoughts or Contribute your information that might help someone here.
Ask your Doubts & Queries to get answers.. "Together, We enlight each other!"
Register FREE for AD-FREE forum, Create your own topics, Ask Questions, track topics, setup subscriptions & notifications and Get a Free Website w/ Email and FTP.
500MB Space *No Ads*, CPanel, FTP, PHP, MySQL, EMails - 100% FREE