Author Topic: Introduction to PHP  (Read 2063 times)

Offline Zeggy

  • Global Moderator
  • Level 35
  • *****
  • Posts: 1,187
  • Reputation: +13/-4
    • View Profile
Introduction to PHP
« on: August 31, 2006, 06:38:38 AM »
What is PHP?

PHP is a server-side scripting language. This means that all the PHP code is processed server-side, unlike HTML, so PHP code is usually not for the public to see. It is used for creating dynamic pages.

Using PHP

To begin with, PHP begins and ends with php tags. Everything within these tags will be processed:
Code: [Select]
<?php

//Code here

?>

Some alternative tags:

Code: [Select]
<?

//Code here

?>

These tags must be specially configured first:
Code: [Select]
<%

//Code here

%>



<SCRIPT LANGUAGE="php">

//Code here

</SCRIPT>


Hello World!

As a first example of using PHP, we will 'echo' the text "Hello World!":

Code: [Select]
<?php

echo "Hello World!";

?>

As you can see, the code begins with the PHP tags, and includes this line:
echo "Hello World!";
It's ended by a semi-colon, which is used to end all statements.
The echo function basically just prints the text within quotes onto the browser screen. Double quotes and single quotes can be used.

Variables

In PHP, variables are used to store data that can be changed. PHP's variables, unlike C/C++ or Java, can be of any type, so can also be used without specifying a type:

Code: [Select]
<?php

$variable 
"I'm a string!";

echo 
$variable;



$variable 1;

echo 
$variable;

?>

In this example, the variable has changed its content and its type, from a string into an integer. Both variables' contents are printed on the screen. In this example, the echo function does not use double or single quotes becuase it only prints out a variable. Anything else, including a combination of variable and text will have to be included within quotes.


Remember

PHP is supposed to be used with HTML. You can echo the HTML within PHP, sure, but it is much better to seperate the HTML and PHP:

Code: [Select]
<html>

<head>

<title>Hello!</title>

</head>

<body>

Hello, <?php

$name 
"Andy";

echo 
$name;

?>


</body>

</html>

This would print "Hello, Andy". The HTML could have been included in the PHP but it becomes a pain to understand the source when the web page becomes more complicated and large.

Offline KingMonkey Throughout!

  • Somebody Told Me!
  • Level 13
  • *
  • Posts: 102
  • Reputation: +3/-17
    • View Profile
Re: Introduction to PHP
« Reply #1 on: September 27, 2006, 10:39:40 AM »
A bit more :)

Include a page
Code: [Select]
<?php
include("File name");
?>

That will run that file on the page you insert that code into
 
Require to run page once!
Code: [Select]
<?php
require_once("File name");
?>

This will Make the code only use the file once.

Show Text

Code: [Select]
<?php
print"Whoooo im great";
?>

That will come up as 'Whoooo im great' on the page that its on. You can also use echo instead of print.
 
PHP Game Programer!


Offline Sava

  • Level 13
  • *
  • Posts: 101
  • Reputation: +3/-15
  • It's just me
    • View Profile
Re: Introduction to PHP
« Reply #2 on: September 28, 2006, 04:07:47 PM »
the elementary but great description. Really nice.

I have one using the include function:


so you have a variable.php
Quote
<?php
$color = 'blue';
$item = 'sky';
?>

and an index.php

Quote
<?php
echo 'The $item is $color'; // and you get only The is
?>

but if you also include the variable.php

Quote
<?php
include('variable.php');

echo 'The $item is $color'; // and you will get the right thing  The sky is blue

?>

Offline smiley

  • Level 13
  • *
  • Posts: 101
  • Reputation: +0/-7
    • View Profile
Re: Introduction to PHP
« Reply #3 on: September 30, 2006, 06:30:39 AM »
I want to learn php.

Offline smiley

  • Level 13
  • *
  • Posts: 101
  • Reputation: +0/-7
    • View Profile
Re: Introduction to PHP
« Reply #4 on: September 30, 2006, 06:40:30 AM »
I want to learn php and hi_118 said i could learn it here.

Offline Mgccl

  • Level 7
  • *
  • Posts: 33
  • Reputation: +1/-0
    • View Profile
    • WebDevLogs
Re: Introduction to PHP
« Reply #5 on: November 09, 2006, 10:54:36 PM »
normally... echo is coooool and fast
unless you have output buffer... then print is faster...

But.. when you got smarty... why do you have to care about echo and print anymore...

 


SimplePortal 2.3.3 © 2008-2010, SimplePortal