FAQ  •   Search   •  Memberlist  •  Usergroups   •  Register   •  Profile  •  Log in to check your private messages  •  Log in
 Lucky Dice  •   Free Image Hosting

 PHP for Newbs - Lesson 1 View next topic
View previous topic
Post new topicThis topic is locked: you cannot edit posts or make replies.
Author Message
RCTycooner
Site Admin


Joined: 26 Feb 2005
Posts: 197
Location: Belgium -> Antwerp -> Merksem
Rupees 1274.61

PostPosted: Fri Sep 07, 2007 10:49 am Reply with quoteBack to top

(I'm following the main lines of the php.net getting started documentation: http://www.php.net/manual/en/introduction.php --> you can often find user's notes there that might clarify things even more)

Any questions about anything, just ask ;)

What is php?

First, you'll have to understand that php is executed on the server. HTML and Javascript are executed on the client's computer (by the internet browser). This is the main difference between php and javascript. --> php code will run BEFORE html is shown by the browser.

What do I need?

You'll need a hosting that provides PHP support. (Most hostings have this)
Optionally, you'll want them to provide a database (eg. MySQL). Do note that this is not a requirement. ;)

Furthermore, you'll need an editor. Notepad will do just fine :) But when you use it, make sure the file is saved as ".php" (default will be ".txt").

Your First script

Well then, now you got something to write php and something to test it, let's make our first script:

Open your editor (eg. notepad) and write the next code:
Code:

<?php
echo 'Hello World!';
?>


Save it as a ".php" file (eg. test.php), upload it to your website and use your webbrowser to go to the file.

You will see something like this:
Code:

Hello World!


Now, let's examine this little script:

The tags <?php and ?> encircle the area where the script is written in php.
This is needed so you can combine php with html.
An example:
Code:

<html>
  <head>
    <title>A PHP Example</title>
  </head>
  <body>
    We put some html up front...
    <br>
<?php
   echo "Followed by some php code";
?>
  </body>
</html>


This will Give a page that looks like this:

Code:

<html>
  <head>
    <title>A PHP Example</title>
  </head>
  <body>
    We put some html up front...
    <br>
    Followed by some php code
  </body>
</html>


As you can see, you can put the php in between the html or vice-versa :)

Now, onto the next part of that piece of code:
"echo" is a function that justs prints out some data.

One thing you'll definatly need to know in php is that every code of line stops with a ";".
This means that you can put code on multiple lines in your editor and it still we be interpreted correctly by the webserver.
An example:
Code:

<?php

echo 'This is
an
example.';

?>

This will show:
Code:

This is an example.


Variables

Variables work in php just like they do in other languages. With the only difference that they can be easily reconised by their $ sign before the name.

The code below shows some examples on how you can use variables:
Code:

<?php

//some integers:
$a = 1;
$b = 5;

//a few things you can do with them:
$c = $a + $b;
$d = $b * $c;

echo $c;
echo "$d contains: " . $d;

//some strings:
$s = "This is a string";
$t = 'This is one too';

echo $s;

//The "." (dot) glues several strings together
echo '<br>'. $s . ' ' . $t;


$this_is_a_variable = 'You can name a variable in several ways.';

?>


You can type commentary between your code by these two ways:
Code:

<?php

// use two slashes "//" to comment a single line of text.

/*
You can start a block of comments with "/*"
But remember to end it with "*/"
*/

?>
View user's profileSend private messageSend e-mailVisit poster's website
Display posts from previous:      
Post new topicThis topic is locked: you cannot edit posts or make replies.


 Jump to:   



View next topic
View previous topic
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2002 phpBB Group :: Original theme by FI Theme :: All times are GMT + 1 Hour

Copyright 2003-2010 © RC-Technologies. All rights reserved. "RC-Technologies", "The Silver Luna" and "The Battle For The Silver Luna" are copyrighted by RC-Technologies.
Bookmark us! Disclaimer. Privacy Policy. We are in no way affiliated with the phpBB Group. phpBB is copyright to the phpBB Group