Second Php Tutorial - Variables

free web hosting
Open Discussion > CONTRIBUTE > Tutorials

Second Php Tutorial - Variables

ghostrider
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://www.ghostrider.trap17.com

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.

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


Variables can also hold numbers:

CODE
<?
$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.

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


HOWEVER, if you put it in single quotes

CODE
<?
$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:

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.

CODE
<?
$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.

 

 

 


Reply

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

Reply

Blessed
Thanx for the tutorial m8
Nice one this explains a lot to me wink.gif

CODE
<?php
$name = "<b>ghostrider</b>";
$text = "Thanx Realy nice tutorial ";

print "$text" . " " . $name . "!";
?>

Notice from jlhaslip:

added code tags.

Reply

truefusion
QUOTE(ghostrider @ Apr 8 2007, 10:57 AM) *
CODE
<?
$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 (/).

QUOTE(ghostrider @ Apr 8 2007, 10:57 AM) *
CODE
<?
$name = "Chris";
$lname = "Feilbach";

print "Hello, $name" . " " . $lname . "!";
?>

You don't have to space everything out. It can be done like so:
CODE
echo "Hello, $name"." ".$lname."!";


Side note: As a coding standard, one shouldn't make a "shortcut" when starting off their script; that is, don't "<?" but rather "<?php".

 

 

 


Reply

shadowx
QUOTE
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.

CODE
<?
$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:

CODE
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?

Reply

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

Reply

shadowx
Aye Aye captain!

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

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 too

echo "<BR><BR>";
echo "Hello, my name is $names[first] $names[last].";

// and with dots

echo "<BR><BR>";
echo "Hello, my name is $names[first]" . " " . $names[last] . ".";
?>


Which outputted:

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

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

Hello, 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...

Reply

ghostrider
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 smile.gif.


Reply

truefusion
@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:
CODE
$var1 = "Hello World!";
$var2 = "I am at the foobar!";

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



QUOTE(ghostrider @ Apr 8 2007, 07:11 PM) *
To truefusion: Thanks for pointing out that I had forward slashes instead of blackslashes. I have corrected it smile.gif.

No problem.

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.


*RANDOM STUFF*





*SIMILAR VIDEOS*
Searching Video's for php, variables

*MORE FROM TRAP17.COM*
advertisement



Second Php Tutorial - Variables



 

 

 

 

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