|
|
|
|
![]() ![]() |
Apr 8 2007, 02:57 PM
Post
#1
|
|
|
Super Member ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() Group: Members Posts: 397 Joined: 9-June 06 From: Wisconsin Member No.: 24,924 |
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. This post has been edited by ghostrider: Apr 8 2007, 11:09 PM |
|
|
|
Apr 8 2007, 03:09 PM
Post
#2
|
|
|
A computer once beat me at chess, but it was no match for me at kick boxing. ![]() Group: [MODERATOR] Posts: 4,077 Joined: 24-July 05 From: Linix, DOS and Windows…the good, the bad and the ugly Member No.: 9,787 ![]() |
Nice Tutorial. Keep them coming. Lots of people will learn from these short little snippets of information.
|
|
|
|
Apr 8 2007, 03:15 PM
Post
#3
|
|
|
Advanced Member ![]() ![]() ![]() ![]() ![]() ![]() ![]() Group: Members Posts: 144 Joined: 22-March 07 Member No.: 40,472 |
Thanx for the tutorial m8
Nice one this explains a lot to me CODE <?php $name = "<b>ghostrider</b>"; $text = "Thanx Realy nice tutorial "; print "$text" . " " . $name . "!"; ?> |
|
|
|
Apr 8 2007, 03:46 PM
Post
#4
|
|
|
Ephesians 6:10-17 ![]() Group: [MODERATOR] Posts: 1,916 Joined: 22-June 05 From: The World of Gentoo Member No.: 8,528 |
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 (/). 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". |
|
|
|
Apr 8 2007, 08:38 PM
Post
#5
|
|
|
A clever man learns from his own mistakes, a WISE man learns from those of OTHERS ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() Group: [HOSTED] Posts: 1,035 Joined: 12-April 06 From: Essex, UK Member No.: 21,719 |
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? |
|
|
|
Apr 8 2007, 09:31 PM
Post
#6
|
|
|
A computer once beat me at chess, but it was no match for me at kick boxing. ![]() Group: [MODERATOR] Posts: 4,077 Joined: 24-July 05 From: Linix, DOS and Windows…the good, the bad and the ugly Member No.: 9,787 ![]() |
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.
|
|
|
|
Apr 8 2007, 10:02 PM
Post
#7
|
|
|
A clever man learns from his own mistakes, a WISE man learns from those of OTHERS ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() Group: [HOSTED] Posts: 1,035 Joined: 12-April 06 From: Essex, UK Member No.: 21,719 |
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... |
|
|
|
Apr 8 2007, 11:11 PM
Post
#8
|
|
|
Super Member ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() Group: Members Posts: 397 Joined: 9-June 06 From: Wisconsin Member No.: 24,924 |
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 |
|
|
|