hosted by liquidweb


Go Back   Web Hosting Talk : Web Hosting Main Forums : Programming Discussion : Programming Tutorials : Basics Of PHP
Reply

Programming Tutorials How-Tos related to programming, databases, and the like.
Forum Jump

Basics Of PHP

Reply Post New Thread In Programming Tutorials Subscription
 
Send news tip View All Posts Thread Tools Search this Thread Display Modes
  #1  
Old 11-12-2005, 09:43 PM
Mr.X Mr.X is offline
Newbie
 
Join Date: Nov 2005
Posts: 23

Basics Of PHP


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
Code:
<?php
Ending
Code:
?>
Baisc Syntax

Now that you know about the starting & ending tag's , let's start with a basic script.
Code:
<?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.
Code:
<?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.
Code:
//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.
Code:
<?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".
Code:
<?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
Code:
while ($z <= 5)
output the value of z
Code:
echo $z
, increase z by 1
Code:
$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.
Code:
<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.
Code:
<?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.

Reply With Quote


Sponsored Links
  #2  
Old 05-29-2006, 02:56 AM
Darkstarx Darkstarx is offline
Newbie
 
Join Date: May 2006
Posts: 9
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!

Reply With Quote
  #3  
Old 05-29-2006, 03:48 AM
KGIII KGIII is offline
Web Hosting Master
 
Join Date: May 2002
Location: NE USA (Almost Canada)
Posts: 990
Just An Extra Note:

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

With a plain text editor paste this in:
Code:
<?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

Reply With Quote
Sponsored Links
  #4  
Old 09-16-2006, 03:10 PM
welshboy welshboy is offline
Newbie
 
Join Date: Sep 2006
Posts: 8
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

Reply With Quote
  #5  
Old 09-17-2006, 07:38 AM
zacharooni zacharooni is offline
Community Guide
 
Join Date: Apr 2005
Posts: 1,214
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 Code:
<?PHP

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

if ($ip == $allowed_ip) {

phpinfo();

} else {

header("Location: /");

}

?>

Reply With Quote
  #6  
Old 10-24-2006, 10:18 PM
CodyRo CodyRo is online now
Web Hosting Master
 
Join Date: Feb 2006
Location: Buffalo NY
Posts: 1,148
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)

Reply With Quote
  #7  
Old 10-25-2006, 01:01 AM
jadebellant jadebellant is offline
New Member
 
Join Date: Oct 2006
Posts: 2
Is there a function in PHP that's similar to CFHTTP? I need to read the contents of one of our sites.

Reply With Quote
  #8  
Old 10-25-2006, 01:03 AM
jadebellant jadebellant is offline
New Member
 
Join Date: Oct 2006
Posts: 2
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?

Reply With Quote
  #9  
Old 10-25-2006, 01:45 AM
ThatScriptGuy ThatScriptGuy is offline
Web Hosting Master
 
Join Date: Feb 2003
Location: AR
Posts: 2,370
If your form is ONLY using the post method, use $_POST. Likewise for $_GET. If your form should accept both methods, use $_REQUEST.
Kevin

Reply With Quote
  #10  
Old 11-14-2006, 04:09 AM
brutetal brutetal is offline
Newbie
 
Join Date: Jun 2006
Location: California
Posts: 5
Good short, tutorial, good for new people wanting to learn PHP.

Next thing functions, classes and arrays!

Reply With Quote
  #11  
Old 12-29-2006, 02:20 PM
horizon horizon is offline
Web Hosting Master
 
Join Date: Mar 2006
Posts: 961
Nice block from SNC. However, I'd state this line:

PHP Code:
$ip getenv("REMOTE_ADDR"); 
for:

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

Reply With Quote
  #12  
Old 12-31-2006, 03:54 PM
FunkyFresh FunkyFresh is offline
Junior Guru Wannabe
 
Join Date: Dec 2006
Location: PA
Posts: 98
thank u so much this is nice short tutorial

Reply With Quote
  #13  
Old 01-07-2007, 02:19 AM
GoodFellas GoodFellas is offline
Newbie
 
Join Date: Oct 2005
Posts: 6
Nicely put togethor, well done

Reply With Quote
  #14  
Old 01-21-2007, 03:44 PM
vkess vkess is offline
Newbie
 
Join Date: Jan 2007
Posts: 10
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

Reply With Quote
  #15  
Old 01-21-2007, 03:46 PM
Biju Biju is offline
Big fan of RajiniKanth!!!
 
Join Date: Sep 2004
Location: Chennai , India
Posts: 4,495
Quote:
Originally Posted by vkess
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.

Reply With Quote
Reply

Related posts from TheWhir.com
Title Type Date Posted
6Scan Sees Surge of Malicious Attempts During WordPress Attack Web Hosting News 2013-04-18 18:36:19
Joomla Hosting Study Shows Speed, Uptime Top Selling Points of a Web Host Web Hosting News 2012-11-07 15:06:36
Smart Emotions Lead To Great Customer Service Blog 2012-06-11 11:14:38
Parallels “Cloud” Summit - It was all about TRUST Blog 2012-03-01 12:29:22
Web Host Rackspace Offers OpenStack Training Program Web Hosting News 2011-08-24 17:05:56


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes
Postbit Selector

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
Login:
Log in with your username and password
Username:
Password:



Forgot Password?
Advertisement:
Web Hosting News:



 

X

Welcome to WebHostingTalk.com

Create your username to jump into the discussion!

WebHostingTalk.com is the largest, most influentual web hosting community on the Internet. Join us by filling in the form below.


(4 digit year)

Already a member?