Go Back   Admin Zone Forums > The Resource Zone > Articles

Articles A Collection of Articles, Interviews, and Other Resources.

Reply
 
Thread Tools

  #1  
Old 04-04-2006, 04:32 AM
harmor harmor is offline
TAZ Regular
 
Real Name: Andrew Harmor
Join Date: Feb 2005
Admin Experience: Advanced
Age: 24
Posts: 77
harmor is on a distinguished road
Default 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 Code:
<?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 Code:
<?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 Code:
<?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 them in quotes is not needed.
If you were to output a string with the variable then you'll need to use quotes
PHP Code:
<?php
$name 
"Andrew Harmor";
echo 
"Hello $name";
?>
As you can see variables and the echo statement are pretty much the same.
You can also assign variables to variables.
That may sound confusing but it works the same way as done in the echo statement.
PHP Code:
<?php
$age 
"20";
$name "Andrew Harmor, $age" 
echo $name;
?>
The variable $age has the value "20" assigned to it.
The variable $name has the value "Andrew Harmor, 20" assigned to it.
So when you output the value of $name you'll see exactly that.

If you want to output the value of $age and $name by themselves you would do...
PHP Code:
<?php
$age 
"20";
$name "Andrew Harmor" 
echo "My name is $name, I'm $age years old";
?>
Having a value respective to the variable name gives you greater control over how you output them.
There are some instances where you have to assign variable(s) to another variable but only do so if it's your only option.
__________________
vHosting
Manage you hosting requests easier


Xen Web
Offering ad-free hosting with features such as, cpanel, fantastico, PHP and MySQL support, and more
Reply With Quote
  #2  
Old 05-16-2006, 08:03 PM
olti olti is offline
TAZ Rookie
 
Join Date: Dec 2005
Admin Experience: Advanced
Posts: 26
olti is on a distinguished road
Default
Thank you, harmor.
Reply With Quote
  #3  
Old 10-30-2006, 02:04 PM
Infamous Flame's Avatar
Infamous Flame Infamous Flame is offline
Ewan McLean
 
Real Name: Ewan
Join Date: Nov 2005
Admin Experience: Advanced
Posts: 93
Infamous Flame is on a distinguished road
 
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.
__________________
http://ewanmclean.com
Reply With Quote
  #4  
Old 10-30-2006, 11:42 PM
Danny.VBT's Avatar
Danny.VBT Danny.VBT is offline
Tazmanian
 
Real Name: Danny
Join Date: Nov 2004
Admin Experience: Advanced
Age: 20
Posts: 150
Danny.VBT is on a distinguished road
Default
Quote:
Originally Posted by Infamous Flame
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.
__________________

Reply With Quote
  #5  
Old 10-31-2006, 02:36 AM
ShadeSlayer's Avatar
ShadeSlayer ShadeSlayer is offline
4 inches twice is 8.
 
Real Name: Alex Crooks
Join Date: Sep 2006
Admin Experience: Guru
Location: Canada
Age: 15
Posts: 83
ShadeSlayer is on a distinguished road
Default
Also, I can offer some PHP help:

PHP Code:
<?
   
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
Reply With Quote
  #6  
Old 11-25-2006, 02:21 AM
nitr021's Avatar
nitr021 nitr021 is offline
T. Quantity Surveyor
 
Real Name: Kapz (bhavesh)
Join Date: Jan 2006
Admin Experience: Advanced
Age: 22
Posts: 1,321
nitr021 is a jewel in the rough
Default
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

$age = '20'; 
$name = 'Andrew Harmor, '.$age;  
echo $name; 
?>
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.
__________________
Forum design & development Blog
Articles, news, tips & many more
Reply With Quote
  #7  
Old 11-25-2006, 06:59 AM
Pyrix's Avatar
Pyrix Pyrix is offline
TAZ Regular
 
Real Name: Ollie
Join Date: Mar 2004
Admin Experience: Advanced
Location: UK
Age: 22
Posts: 61
Pyrix is on a distinguished road
Default
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
Reply With Quote
  #8  
Old 01-15-2007, 04:19 PM
Zerce Zerce is offline
TAZ Regular
 
Real Name: ???
Join Date: Aug 2005
Admin Experience: Guru
Posts: 55
Zerce is on a distinguished road
Default
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...
Reply With Quote
  #9  
Old 01-15-2007, 09:26 PM
Danny.VBT's Avatar
Danny.VBT Danny.VBT is offline
Tazmanian
 
Real Name: Danny
Join Date: Nov 2004
Admin Experience: Advanced
Age: 20
Posts: 150
Danny.VBT is on a distinguished road
Default
Quote:
Originally Posted by Zerce View Post
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 Code:
echo 'My name is ' $name ', I'' . $age . ' years old
PHP will look for variables inside double quotes, with single quotes you will speed up processing time.
__________________

Reply With Quote
  #10  
Old 01-16-2007, 11:35 AM
KeithMcL's Avatar
KeithMcL KeithMcL is offline
Freelance Web Designer
 
Real Name: Keith
Join Date: Jan 2004
Admin Experience: Advanced
Location: Dublin, Ireland
Age: 35
Posts: 2,866
KeithMcL has a spectacular aura about
Default
Quote:
Originally Posted by Danny.VBT View Post
You would also want to use single quotes in the above case:

PHP Code:
echo 'My name is ' $name ', I'' . $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 Code:
echo 'My name is ' $name ', I\'m ' $age ' years old'
Reply With Quote
  #11  
Old 01-16-2007, 02:38 PM
harmor harmor is offline
TAZ Regular
 
Real Name: Andrew Harmor
Join Date: Feb 2005
Admin Experience: Advanced
Age: 24
Posts: 77
harmor is on a distinguished road
Default
You don't need to concatenate variables. You only concatenate arrays

PHP Code:
echo "My name is ".$person['name'].", I'm ".$person['age']." years old"
__________________
vHosting
Manage you hosting requests easier


Xen Web
Offering ad-free hosting with features such as, cpanel, fantastico, PHP and MySQL support, and more
Reply With Quote
  #12  
Old 01-17-2007, 02:42 AM
Brad's Avatar
Brad Brad is offline
Caffeine Addict
 
Real Name: B-rad
Join Date: Jan 2004
Admin Experience: Advanced
Location: Cardiac Cat Country
Age: 23
Posts: 1,837
Brad is a jewel in the rough
Default
Quote:
Originally Posted by harmor View Post
You don't need to concatenate variables. You only concatenate arrays

PHP Code:
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 Code:
print('Hi my name is' $name); 
Also you can use an array inside of the double quotes if you do this instead;

PHP Code:
print("My name is {$person['name']}, I'm {$person['age']} years old"); 
Reply With Quote
  #13  
Old 01-18-2007, 12:29 PM
nitr021's Avatar
nitr021 nitr021 is offline
T. Quantity Surveyor
 
Real Name: Kapz (bhavesh)
Join Date: Jan 2006
Admin Experience: Advanced
Age: 22
Posts: 1,321
nitr021 is a jewel in the rough
Default
Quote:
Originally Posted by harmor View Post
You don't need to concatenate variables. You only concatenate arrays

PHP Code:
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.
__________________
Forum design & development Blog
Articles, news, tips & many more
Reply With Quote
  #14  
Old 01-18-2007, 01:24 PM
Brad's Avatar
Brad Brad is offline
Caffeine Addict
 
Real Name: B-rad
Join Date: Jan 2004
Admin Experience: Advanced
Location: Cardiac Cat Country
Age: 23
Posts: 1,837
Brad is a jewel in the rough
Default
Quote:
Originally Posted by nitr021 View Post
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

Reply With Quote
  #15  
Old 01-18-2007, 01:33 PM
nitr021's Avatar
nitr021 nitr021 is offline
T. Quantity Surveyor
 
Real Name: Kapz (bhavesh)
Join Date: Jan 2006
Admin Experience: Advanced
Age: 22
Posts: 1,321
nitr021 is a jewel in the rough
Default
ty for that.
__________________
Forum design & development Blog
Articles, news, tips & many more
Reply With Quote
  #16  
Old 01-24-2007, 09:02 AM
nichky's Avatar
nichky nichky is offline
TAZ Rookie
 
Real Name: Nicholas
Join Date: Jan 2007
Admin Experience: Beginner
Posts: 8
nichky is on a distinguished road
Default
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,

Last edited by nichky; 01-24-2007 at 09:07 AM..
Reply With Quote
Reply





Currently Active Users Viewing this Thread: 1 (0 members and 1 guests)
 
Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Interview with Dietrich Moerman - UseBB Creator The Sandman Interviews 4 09-21-2009 07:43 PM
Interview with Brian Moon The Sandman Interviews 11 01-24-2009 02:40 AM
Learning PHP Lee Davies Chit Chat 9 02-06-2005 08:55 AM
Interview: Logician [vBulletin.org] The Sandman Interviews 4 08-04-2004 04:14 PM
Learning PHP...? Bryan Chit Chat 12 03-23-2004 10:53 PM



All times are GMT -4. The time now is 08:39 AM.


Powered by: vBulletin
Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.
Page generated in 0.38482094 seconds with 11 queries
The Admin Zone © copyright 2003-2010 All Rights Reserved. Content published on The Admin Zone requires permission for reprint.