| Articles A Collection of Articles, Interviews, and Other Resources. |
![]() |
|
|
Thread Tools |
|
#1
|
|||
|
|||
|
|
|||
|
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:
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:
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:
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:
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:
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:
There are some instances where you have to assign variable(s) to another variable but only do so if it's your only option. |
|||
|
#2
|
|||
|
|||
|
|
|||
|
Thank you, harmor.
|
|||
|
#3
|
||||
|
||||
|
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 |
||||
|
#4
|
||||
|
||||
|
|
||||
|
Quote:
|
||||
|
#5
|
||||
|
||||
|
|
||||
|
Also, I can offer some PHP help:
PHP Code:
|
||||
|
#6
|
||||
|
||||
|
|
||||
|
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; ?> it has a manual. Depending on what you do, just search it and you'll find something. |
||||
|
#7
|
||||
|
||||
|
|
||||
|
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 |
||||
|
#8
|
|||
|
|||
|
|
|||
|
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... |
|||
|
#9
|
||||
|
||||
|
|
||||
|
Quote:
PHP Code:
|
||||
|
#10
|
||||
|
||||
|
|
||||
|
PHP Code:
|
||||
|
#11
|
|||
|
|||
|
|
|||
|
You don't need to concatenate variables. You only concatenate arrays
PHP Code:
|
|||
|
#12
|
||||
|
||||
|
|
||||
|
PHP Code:
PHP Code:
|
||||
|
#13
|
||||
|
||||
|
|
||||
|
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. |
||||
|
#14
|
||||
|
||||
|
|
||||
|
Quote:
|
||||
|
#15
|
||||
|
||||
|
|
||||
|
ty for that.
|
||||
|
#16
|
||||
|
||||
|
|
||||
|
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.. |
||||
![]() |
| Currently Active Users Viewing this Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
|
|
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 |
