Web Hosting Talk







View Full Version : Basics Of PHP


Mr.X
11-12-2005, 09:43 PM
I figured it's time to make a small tutorial , by reading this tutorial you should have the basic know how to execute small script's on your php enabled servers. Note that i will not be explaining how to set up a web server + php.

What Does PHP Stand For?

PHP- Hypertext Preprocessor

Starting & Ending Tags

We start off by learning the 2 main tag's that you must never forget , that is the starting and ending tag's.

Starting <?php Ending ?>
Baisc Syntax

Now that you know about the starting & ending tag's , let's start with a basic script.

<?php
echo "This is my first php script!";
?>

so you have noticed echo , echo is simple, it output's the text "This is my first php script!" , now you must end this with a semicolon , why? the semicolon is a seperator and is used to distinguish one set of instructions from another.

Variables

Now that you know some basic syntax let's start working on variables , variables are ton's of fun and one of the most important part's of PHP.

<?php
$myfirstscript = "This is my first php script!";
echo $myfirstscript;
?>
So you have noticed $myfirstscript , i made the variable $myfirstscript that hold's the string "This is my first php script!" . Then ofcourse just echo'd the variable which would output "This is my first php script!".

Comments

Comments are also another important thing not only in php but all languages , there used to disable any type of information inside them from actually executing with your script which ofcourse would likely cause error's.

//This is a comment
/*This is
a
comment block*/

Condition Statements

Now these are fun to play with , they are used to execute certain code on a condition.

<?php
$mybrowser = "Fire Fox";
if ($mybrowser == "Fire Fox") { echo " You are using firefox!"; }
else { echo " You are not using firefox"; }
?>
So here i have set a variable equal to my browser then made a condition, if my browser is equal to firefox, output "You are using firefox" else (if your using something else) output "You are not using firefox" . Ofcourse this will not work untill i i add more to it but it give's you all a idea how condition statements work.

Looping

Looping is used for many many thing's , there are more then just one loop statement, ill explain how to do the "While Loop".

<?php
$z = 1;
while ($z <= 5) {
echo $z;
$z++
}
Again i made a variable named z with the value of 1 , then stated while z is less or equal to 5 while ($z <= 5)output the value of z echo $z , increase z by 1$z++. Which would output 1-5 on your page. Simple isn't it?

Forms

Now im assuming you have some basic understanding of html, although i will give an example of the html form but i will not be explaining it.

<form action="form.php" method="post">
Name: <input type="text" name="name">
<input type="submit" value="Submit">
That's the html form , very simple. Now to the fun part.

<?php
$name = $_POST["name"];
echo "Hello ". $name;
?>
Now here you have noticed something new and that would be "$_POST["name"]", $_POST is a variable already set by php , it recieved the information givin in the html form , notice the name="name" in the html form and ["name"] in $_POST.

Now this is the end of this small basic tutorial , you should now have the knowledge to execute simple script's on your server's.

Darkstarx
05-29-2006, 02:56 AM
I would suggest editing $mybrowser = "Fire Fox"; to something less browser related. There are actually people out there who might think this code determines what browser is being used!

KGIII
05-29-2006, 03:48 AM
Just An Extra Note:

If you would like to get the PHP information from your site:

With a plain text editor paste this in:

<?php phpinfo() ?>


Name it "something.php" (or even info or phpinfo.php) and upload and then open in your browser.

Figured that'd be good for folks who are just starting on the basics and if it's covered anywhere here already I'm not really sure.

KGIII

welshboy
09-16-2006, 03:10 PM
nice one, ive already learnt this but i havent used php in a while so its nice to refresh my memory abit, great post for people who want to get into php

zacharooni
09-17-2006, 07:38 AM
Actually for a phpinfo() I would do this, since it volunteers a lot of information about your server, that you may not neccessarily want being public.


<?PHP

$ip = getenv("REMOTE_ADDR");
$allowed_ip = "24.211.80.102"; // change to your ip

if ($ip == $allowed_ip) {

phpinfo();

} else {

header("Location: /");

}

?>

CodyRo
10-24-2006, 10:18 PM
Or just create the file when needed / don't name it phpinfo.php :)

(Or just add a constant to the index.php to show phpinfo if defined, etc)

jadebellant
10-25-2006, 01:01 AM
Is there a function in PHP that's similar to CFHTTP? I need to read the contents of one of our sites.

jadebellant
10-25-2006, 01:03 AM
Is it better to use $_request instead of $_POST or $_GET? Since it does what $_POST and $_GET do anyway. Is there a drawback to using it?

ThatScriptGuy
10-25-2006, 01:45 AM
If your form is ONLY using the post method, use $_POST. Likewise for $_GET. If your form should accept both methods, use $_REQUEST.
Kevin

brutetal
11-14-2006, 04:09 AM
Good short, tutorial, good for new people wanting to learn PHP.

Next thing functions, classes and arrays!

horizon
12-29-2006, 02:20 PM
Nice block from SNC. However, I'd state this line:


$ip = getenv("REMOTE_ADDR");


for:


$ip = (isset($_SERVER['REMOTE_ADDR'])) ? $_SERVER['REMOTE_ADDR'] : getenv("REMOTE_ADDR");


;)

FunkyFresh
12-31-2006, 03:54 PM
thank u so much this is nice short tutorial

GoodFellas
01-07-2007, 02:19 AM
Nicely put togethor, well done

vkess
01-21-2007, 03:44 PM
Anyone seriously looking to learn PHP should bookmark the site below. If you have any questions they can usually be answered there.

http://www.php.net/

The function reference is particularly useful.

http://www.php.net/manual/en/funcref.php

Biju
01-21-2007, 03:46 PM
Anyone seriously looking to learn PHP should bookmark the site below. If you have any questions they can usually be answered there.

http://www.php.net/

The function reference is particularly useful.

http://www.php.net/manual/en/funcref.php

There is no better books than the PHP manual, but the only problem is most of people find it to read e books.

admin-globalvidia
02-13-2007, 10:34 PM
Hi, I'm PHP beginner. Could you advice me any free PHP engine for my site?
Thank you

horizon
02-14-2007, 12:04 AM
PHP engines are mostly programmed for specific concepts. What kind of PHP engine are you looking for actually ?

stridox
02-17-2007, 12:23 PM
OpenSourceCMS (http://www.opensourcecms.com/) has a lot of free scripts, try looking there

so does resourceindex.com (http://php.resourceindex.com/)

horizon
02-22-2007, 07:43 PM
I received an email notification regarding a new post on this topic ... not too sure how new it is . . . have I missed something ?

horizon
03-03-2007, 05:57 PM
Yeah, it pretty much looked like this one:


hmmmmmmmmmmmm

ain't it faizanno1 ?? :eek:

beeb
07-30-2008, 05:39 AM
Thanks, I really want to learn PHP but my head just won't get around it haha.

Simply-hosting
08-12-2008, 06:49 AM
Nice Tutorial , good for teh beginers

d0mer
08-22-2008, 05:27 PM
Besides knowing what it is, I really don't know where to really start for learning PHP. I have a fair outline of HTML and grasp the basics of CSS (I only know how to work with Dreamweaver). How did you guys learn PHP? Any recommended book?

asuhot
09-13-2008, 04:23 PM
Well, nice tutorial for the beginners who are just starting to wet their feet with php. After going through this basic quickie, they must go on to learn further through other resources available on the net.

limpis
09-16-2008, 11:57 PM
thanks for the info

rsanek
10-05-2008, 07:21 PM
Anyone seriously looking to learn PHP should bookmark the site below. If you have any questions they can usually be answered there.

php.net/

The function reference is particularly useful.

php.net/manual/en/funcref.php

It's a great reference for experienced programmers, but I wouldn't recommend it to people just starting out. It's way too technical and expects you to have an understanding of php.

Bandit-X
10-08-2008, 10:16 AM
the function reference is a bit technical but there is also the getting started and language reference sections.

http://www.php.net/manual/en/getting-started.php

Young Coder
04-13-2009, 09:23 AM
thanks , great tutorial

Eoin_
04-13-2009, 09:41 AM
PHP actually stands (or did stand) for Personal Home Page, but that obviously didn't sound professional enough.

Thanks for the tutorial though :)

DHD-Chris
04-13-2009, 10:32 AM
I leant PHP on W3schools website, I suggest you give them a look up!

Shinary
04-15-2009, 10:38 AM
PHP actually stands (or did stand) for Personal Home Page

PHP stood for "Personal Home Page" until the end of PHP version 3 or
so, when Zend took over the majority of it's development. Presently
it's the recursive acronym "PHP: Hypertext Preprocessor", or something
like that.

As far as learning PHP, once I had the basics of the language down I
started looking at some commonly used open source projects such as
PhpMyAdmin, PhpSysInfo, and Wordpress. I tried to think of a very
simple fix/refactor/new feature and I spent a weekend trying to
implement it.

Also, if you are new to programming in general (and are hoping to make
a career out of it) I would highly advise against diving into PHP
first. Take a look at C++, Java, or even Python to understand the
basic concepts of programming, and then dive into PHP. It may take
awhile but it will make you a much better programmer in the end.

Flumps
05-03-2009, 04:32 PM
another vote for w3schools.com

thats where i learnt most of my php from. php.net is only useful really if you know what your looking for abit a part of php and that your not sure off.

I would highly suggest starting with w3schools. google it. learn it. job done.

use php.net as a reference.

mkcitizen
05-27-2009, 05:42 AM
U have mentioned the basic programming for php. Can you tell me how to save this file & execute it.
I need the extension & shall i execute it on the browser

afgunz
07-25-2009, 03:14 PM
thanks alot, really usefull.

risoknop
07-25-2009, 08:00 PM
First of all, I suggest abiding to some coding standards and/or best practices, one line condition statements for example aren't recommended.


<?php

if (true === $bool) {
// $bool is true
} else if (false === $bool) {
// bool is false
}


Secondly, you should use enctype attribute with HTML forms for security reasons, for instance:


<form enctype="application/x-www-form-urlencoded" method="post" action="/controller/action">


Also use labels for accessibility and don't forget to escape all HTML output (with htmlentities() for example):


// always escape HTML output
echo 'Hello ', htmlentities($_POST['name']);

aliceandmosee056
03-30-2010, 12:57 AM
Thanks man I needed a 'lay-man' terms tutorial because of how new I am to the language. I want to eventually make a browser RPG so this helped a ton man :)

GeeksHosts
03-30-2010, 01:08 AM
Thanks for the nice little tutorial. This will help many New comers.

authority216
09-12-2011, 06:02 AM
Thanks for posting the basics - i'm new to programming and most tutorials seem to assume that you know things like php start and end tags.

amirymk
10-13-2011, 02:55 PM
Thank you for good topic

suvy
10-18-2011, 08:56 AM
Brilliant post!!
really its very awesome and useful tips for me. I do not know PHP but it seems interesting so I definitely start learning in a few days.
Thank you

Swift334
10-19-2011, 05:24 PM
PHP for me is really what ever I make of it. I make it easy for me by using the easiest methods for me as far as getting things done.

Sarushan
11-01-2011, 05:18 PM
Nice, very simple and plain tutorial for new beginners to understand

For people who have moved on from this: I recommend checking out nettutplus and w3schools for more indepth on PHP

DanIonescu
12-01-2011, 04:59 AM
A nice way to learn PHP is by reading code done by somebody else. That's how lots of programmers learnt.

DEREKTROTTER
12-15-2011, 10:16 AM
nice tut, understand it a little better now :D

Mad_matt
12-15-2011, 10:39 AM
great work, thanks. i need all the php tutorials i can get right now

HosterSlice
12-16-2011, 06:42 PM
The best place for help would be http://w3schools.com/

Host-Shield
01-07-2012, 08:49 AM
Very nice tutorial, I'm wanting I start coding PHP this is a great help thank you :)

HobsonsChoice
01-20-2012, 08:52 AM
Hi,

Thank for such a awesome short tutorial,I am seriously going to learn php at last.Do you recommend me any website or ebook for Advanced learning, Video tutorial will be best for me.

Thank you

jenok
01-20-2012, 12:04 PM
Hi,

Thank for such a awesome short tutorial,I am seriously going to learn php at last.Do you recommend me any website or ebook for Advanced learning, Video tutorial will be best for me.

Thank you

http://www.php.net/download-docs.php :agree:

ServSlots
01-22-2012, 01:45 AM
I have been learning php for sometime now and this tutorial is so good for starters