Jump to content
xisto Community
Sign in to follow this  
ghostrider

Second Php Tutorial - Variables

Recommended Posts

If you haven't read the first one, read it by clicking here.

I got a lot of positive comments about my last tutorial. Do you guys think I should keep writitng these?

Intro To PHP
Tutorial 2 - Variables
Released 4/06/07
By Chris Feilbach aka GhostRider

Contact Info:
E-mail: assembler7@gmail.com
AIM: emptybinder78
Yahoo: drunkonmarshmellows
Website: http://forums.xisto.com/no_longer_exists/

It has been a very long time since I have released a tutorial. I've been quite busy working on my newest script for handling news (GAMES), and redesigning my webpage.

This tutorial will cover variables, which are what programmers use to store data in. Make sure that you have read the first tutorial, and that you understand everything in this tutorial as well, as this is one of the core fundamentals in any language that you learn.

Anyway you choose to look at it, computer programming is all about manipulating and displaying data. Even the most simple program possible, has to deal with data, the CPU has to read the data, process it, and then send an output. In a computer game, the computer is inputing data from somewhere (keyboard, mouse, joystick), and outputing data thats based on its inputs (screen and speakers). This makes variables, the things that hold data very very important to use and to understand.

Variables in PHP can either hold data or numbers. Numbers are also sometimes called integers. To tell PHP that your variable is truly a variable, put a dollar sign ($) in front of it, like so.

<?$var1 = "Hello.";?>

Variables can also hold numbers:

<?$var2 = 22;$var3 = 67.43009;?>

One of the most important things in PHP is understanding the difference between strings (a bunch of letters/numbers) in double quotes ("") and single quotes (''). Strings in double quotes allow you to put other variables inside of the string. PHP will then translate that variable into its value. Single quotes will not do this.

For example, lets say that you have a simple script that asks for the persons first name via a form on another page, and then displays it. The name of the person will be stored in $name.

<?$name = "Chris";print "Hello, $name!"; // Will display Hello, Chris!?>

HOWEVER, if you put it in single quotes

<?$name = "Chris";print 'Hello, $name!'; // Will display Hello, $name!?>

This is a very important thing to understand, so make sure you really get it.

Inside a quote you can only have two quote marks, one that signifies the beginning of the string, and another that signifies the end. But what if you to want to use quotes inside of a string?

The solution is simple, lets say you want to put the persons name in quotes (""). Use a backslash (\) in front of the quotes inside of the string to avoid parse errors.

Here would be your code:

<?$name = "Chris"; // This could also be $name = 'Chris';print "Hello, \"$name\"!"; // Will display Hello, "Chris"!?>

There is one more concept that I want to cover before I conclude this tutorial. Strings can be joined using a period *. Lets say that you have a last name, too.

<?$name = "Chris";$lname = "Feilbach";print "Hello, $name" . " " . $lname . "!";?>

Be sure to review this stuff, and make sure you really know it. Next week, or whenever I release my next tutorial, we will cover form data, and conditional statements.
Edited by ghostrider (see edit history)

Share this post


Link to post
Share on other sites

Nice Tutorial. Keep them coming. Lots of people will learn from these short little snippets of information.

Share this post


Link to post
Share on other sites

Thanx for the tutorial m8
Nice one this explains a lot to me ;)

<?php$name = "<b>ghostrider</b>";$text = "Thanx Realy nice tutorial ";print "$text" . " " . $name . "!";?>
Notice from jlhaslip:

added code tags.

Share this post


Link to post
Share on other sites

<?$name = "Chris"; // This could also be $name = 'Chris';print "Hello, /"$name/"!"; // Will display Hello, "Chris"!?>
Back slashes (\) are used to escape characters, not forward slashes (/).

<?$name = "Chris";$lname = "Feilbach";print "Hello, $name" . " " . $lname . "!";?>
You don't have to space everything out. It can be done like so:
echo "Hello, $name"." ".$lname."!";
[hr=noshade]Side note: As a coding standard, one shouldn't make a "shortcut" when starting off their script; that is, don't "<?" but rather "<?php".[/hr]

Share this post


Link to post
Share on other sites

here is one more concept that I want to cover before I conclude this tutorial. Strings can be joined using a period *. Lets say that you have a last name, too.

<?$name = "Chris";$lname = "Feilbach";print "Hello, $name" . " " . $lname . "!";?>

Id like to ask a question, is it necessary to use a dot to join variables? See if i was doing that same script i would just use ECHO and write something like:

echo "$name $lname";

Though i realise that with an array the dot is very useful as arrays dont like being inside quotes so the above thing wouldnt work for that but is it simply good practice to use the dot or does it serve some more important function?

Share this post


Link to post
Share on other sites

Shadowx, try both methods and report back to tell us if they both work the same or not. Then we'll all know which one works the best.

Share this post


Link to post
Share on other sites

Aye Aye captain!

I did a little test as suggested and one using arrays too, just to check, i used this code:

<?$name1 = "Easter";$name2 = "Bunny";$names['first'] = "Easter";$names['last'] = "Bunny";Echo "Hello my name is $name1 $name2. I'm going to kill you all! Muahahaha";//get a fresh line..echo "<BR><BR>";echo "Hello, my name is $name1" . " " . $name2 . ". I'm going to kill you all! Muahahaha";//lets test an array tooecho "<BR><BR>";echo "Hello, my name is $names[first] $names[last].";// and with dotsecho "<BR><BR>";echo "Hello, my name is $names[first]" . " " . $names[last] . ".";?>

Which outputted:

Hello my name is Easter Bunny. I'm going to kill you all! MuahahahaHello, my name is Easter Bunny. I'm going to kill you all! MuahahahaHello, my name is Easter Bunny.Hello, my name is Easter Bunny.

So as you can see the dots dont have any effect, i was surprised that the arrays worked inside double quotes, and also that adding single quotes between the square brackets caused an error so i had to leave them out. I wonder if older versions of php need the dots in some situations and its just a good idea to use them just in case...

Share this post


Link to post
Share on other sites

To shadowx: You don't need a period to join two variables. I just couldn't think of any other way to demonstrate joining strings together.To truefusion: Thanks for pointing out that I had forward slashes instead of blackslashes. I have corrected it ;).

Share this post


Link to post
Share on other sites

@shadowx:
One good reason for using dots instead of placing it all into a string is so you can call a function along with it. For example:

$var1 = "Hello World!";$var2 = "I am at the foobar!";echo $var1." ".strtoupper($var2); //Would echo: Hello World! I AM AT THE FOOBAR!

[hr=noshade]

To truefusion: Thanks for pointing out that I had forward slashes instead of blackslashes. I have corrected it ;).

No problem.[/hr]

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
Sign in to follow this  

×
×
  • 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.