Learning PHP for the first time

harmor

Enthusiast
Joined
Feb 28, 2005
Messages
154
harmor submitted a new Article:

Learning PHP for the first time

When you're learning a programming language you may become overwelmed with the complexity.
I was very overwelmed when I started to learn PHP for the first time over a year ago.
After I grasped how the syntax worked I could learn new methods more easily.

In this article I'll be giving you tutorials on PHP.
Inorder for you to run the PHP scripts you must have a host that supports PHP (atleast 4.1.0).
If you need hosting you can go to Xen Web Hosting and recieve hosting there.
I highly recommend Crimson Editor for a PHP editor

-- Writing your first PHP program --
Any file that contains PHP must have the extension .php (eg. test.php)
Open Crimson Editor and start a new document (Ctrl + n).

The first thing I'll show you is how to output text using "echo".
To output (or print) text to the browser you can use several statements.
In this article I'll be using the "echo" statement.
Outputting text using PHP is pointless in my opinion but beginners need to start somewhere.
PHP:
<?php
echo "This is a string";
?>
The word "echo" tells PHP to output the text between the two quotes to the screen.
The semicolon tells PHP to end the statement (in this case the "echo" statement)
I recommend that you write more "echo" statements to familiarize yourself with the syntax.

I've seen scripts where the coder repeatively uses the "echo" statement for a block of text.
To give you an idea of what I mean imagine copying the "echo" statement and pasting multiple instances of it.
Doing that is unneccesary. In my opinion it makes the code look sloppy.
The way to output a block of text is presented below
PHP:
<?php
echo "Hello, my name is Andrew Harmor
I'm 20 years old and I'm attending Waynesburg College
located in Pennsylvania";
?>
-- Variables --
Variables are going to be your friend because you'll be using them a lot.
The naming convention of a variable consists of the dollar sign and almost any name (eg. $header)
A variable cannot start with a number and it cannot have a special character anywhere in the variable name.
PHP:
<?php
$name = "Andrew Harmor";
echo $name;
?>
The equals sign assigns the string to the variable. So when you "echo" (output) the variable it'll display
the string that is assigned to that variable.
You may have noticed that the "echo" statement does not contain any quotes.
When dealing with variables encasing...

Read more about this article here...
 
Last edited by a moderator:

Infamous Flame

Ewan McLean
Joined
Nov 1, 2005
Messages
186
I didn't read the whole thing lol, but wouldn't echoing:

"My name is $name, I'm $age years old"

Simply output exactly that? (My name is $name, I'm $age years old)

You'd have to use concatenation to make it work properly?

I could be wrong though.
 

Danny.VBT

Adherent
Joined
Nov 7, 2004
Messages
300
Infamous Flame said:
I didn't read the whole thing lol, but wouldn't echoing:

"My name is $name, I'm $age years old"

Simply output exactly that? (My name is $name, I'm $age years old)

You'd have to use concatenation to make it work properly?

I could be wrong though.

Nope - php parses variables inside double strings. Im some stricter languages you would have to concatenate the variable.
 

ShadeSlayer

4 inches twice is 8.
Joined
Sep 25, 2006
Messages
166
Also, I can offer some PHP help:

PHP:
<?
   include("databaseconnecter.php");
   $navigation[] = array("name" => "Blah",
                                "url"  => $PHP_SELF);

// In this PHP code, it does nothing until you make the other files. But this could be a default layout for your pages.

$output= "Why Halo Thar\n"
            ."What is up with /you\n"

	 global $userdata;
	 $replace[] = array("/you", "".$userdata[displayname]."");
if(checkAccess("accessadmin"))
{
$output= "<A HREF='edit$PHP_SELF'>Edit This File</A>\n"
}
else
{
$output= "<B>This part is for admins only</B>\n"

   $pagecontents = $output;
   include("main.css");
   include("layout.php");
?>

BTW, that is for people who know PHP and MySQL and such. I doubt a beginner will know what it does, and I doubt it does anything unless you, ofcourse have the right files :p
 

Bhavesh Ramburn

Quantity Surveyor
Joined
Jan 19, 2006
Messages
2,757
Can I also add that if you are looking for performance the script example provided in this tutorial can take up a few milliseconds to load as it is using ""

try using single quotes like this:
Code:
<?php

[COLOR=#000000][COLOR=#0000bb]$age [/COLOR][COLOR=#007700]= [/COLOR][COLOR=#dd0000]'20'[/COLOR][COLOR=#007700]; 
[/COLOR][COLOR=#0000bb]$name [/COLOR][COLOR=#007700]= '[/COLOR][COLOR=#dd0000]Andrew Harmor, '.$age;  
[/COLOR][COLOR=#007700]echo [/COLOR][COLOR=#0000bb]$name[/COLOR][COLOR=#007700]; [/COLOR][/COLOR]
?>

I also would like to say that the best place to start php is at php.net
it has a manual. Depending on what you do, just search it and you'll find something.
 

Pyrix

Enthusiast
Joined
Mar 15, 2004
Messages
122
I found that one of the best ways to learn was to start looking at other people's code. I took over a custom written website from a friend, and basically rewrote the whole thing using his code as a basis.

I knew the very basics, but because i knew roughly how computers worked logically, and the fact that, for example, 60 queries on a page was bad, i knew what needed to be changed, and all i had to do was find out how to change it and could look it up in a manual. Most of the logic was already there.

Because i was trying to improve existing code (which was pretty poorly written if i'm honest!) i was very aware of bad practises and i think this made my coding a lot better.

Ollie
 

Zerce

Enthusiast
Joined
Aug 26, 2005
Messages
110
For newbies who don't know what concatenate variables is.... here's an explanation.

Instead of doing
echo "My name is $name, I'm $age years old";

You would do
echo "My name is " . $name . ", I'm " . $age . " years old";

That way the variable is not inside the quotes and you can make sure that php will parse it. This is better to do it to make sure you don't get any errors, especially when it is a downloadable script, where people may have different php versions installed or different server types that will treat it differently.

Kinda like XHTML... like <BR /> is not the same as <br /> etc...
 

Danny.VBT

Adherent
Joined
Nov 7, 2004
Messages
300
For newbies who don't know what concatenate variables is.... here's an explanation.

Instead of doing
echo "My name is $name, I'm $age years old";

You would do
echo "My name is " . $name . ", I'm " . $age . " years old";

That way the variable is not inside the quotes and you can make sure that php will parse it. This is better to do it to make sure you don't get any errors, especially when it is a downloadable script, where people may have different php versions installed or different server types that will treat it differently.

Kinda like XHTML... like <BR /> is not the same as <br /> etc...

You would also want to use single quotes in the above case:

PHP:
echo 'My name is ' . $name . ', I'm ' . $age . ' years old';

PHP will look for variables inside double quotes, with single quotes you will speed up processing time.
 

KeithMcL

Freelance Web Designer
Joined
Jan 12, 2004
Messages
5,728
You would also want to use single quotes in the above case:

PHP:
echo 'My name is ' . $name . ', I'm ' . $age . ' years old';
PHP will look for variables inside double quotes, with single quotes you will speed up processing time.
When using single quotes you need to be careful that you escape characters (with a backslash) that you want to be shown in the text (like the ' in I'm).

PHP:
echo 'My name is ' . $name . ', I\'m ' . $age . ' years old';
 

harmor

Enthusiast
Joined
Feb 28, 2005
Messages
154
You don't need to concatenate variables. You only concatenate arrays

PHP:
echo "My name is ".$person['name'].", I'm ".$person['age']." years old";
 
Joined
Jan 6, 2004
Messages
5,956
You don't need to concatenate variables. You only concatenate arrays

PHP:
echo "My name is ".$person['name'].", I'm ".$person['age']." years old";

I like to end quoting as soon as there is no more text to be passed, I do a lot of things like this in my code these days even when using double quotes;

PHP:
print('Hi my name is' . $name);

Also you can use an array inside of the double quotes if you do this instead;

PHP:
print("My name is {$person['name']}, I'm {$person['age']} years old");
 

Bhavesh Ramburn

Quantity Surveyor
Joined
Jan 19, 2006
Messages
2,757
You don't need to concatenate variables. You only concatenate arrays

PHP:
echo "My name is ".$person['name'].", I'm ".$person['age']." years old";
ok seriously that sounds stupid.
using "" in php slows your script down by milliseconds and uses a few more bytes to process as php needs to find the variables within "".
It is fine to use "" if you have some low traffic webpage where only 1-10 people visits it. But if you are aiming your script to be used on high end of the market where 1000's of page view will be generated then you would need to use single quotes to save money and time.

your way is just bad practice.
 
Joined
Jan 6, 2004
Messages
5,956
ok seriously that sounds stupid.
using "" in php slows your script down by milliseconds and uses a few more bytes to process as php needs to find the variables within "".
It is fine to use "" if you have some low traffic webpage where only 1-10 people visits it. But if you are aiming your script to be used on high end of the market where 1000's of page view will be generated then you would need to use single quotes to save money and time.

your way is just bad practice.
http://us3.php.net/manual/en/function.print.php#66392

:)
 

nichky

Neophyte
Joined
Jan 17, 2007
Messages
8
uhm, if you would pardon my newbie-ish question... by "220 posts" for the premium small plan provided by Xen Hosting, does it mean that my forum is restricted to only 220 posts?

edit: don't mind me, i found out what it means already, :p
 
Last edited:
Top