Top
Phpuniverset logo
 

INDHOLD

Forside

Job annoncer

Tips og Tricks

Konkurrence

Downloads

Dokumenter

PHP bog shop

Forum

Online udviklere


Propelcom

ARTIKLER

CSS

Diverse

E-handel / annoncer

Grafik

Html

Internet

Javascript

Linux

PHP/MySQL

Programmer

Flash

Søgemaskiner

Windows

C / C++


INFO

Information

Annoncering

Kontakt

Introduktion til PHP (eng)

Forside Kategori PHP/MySQL Introduktion til PHP (eng)




 

PHP, or Hypertext Preprocessor, is a embedded server-side scripting language. Not only is PHP's popularity growing among web designers, but its features are growing with the release and development of PHP4. This article will cover the aspect of basic programming with PHP3, however, since PHP4 is only in the Beta 3 stage. PHP is embedded straight into HTML code, and is automatically parsed when the viewer loads a document. It is mostly used as a module with the Apache web server for Linux; however, it can easily be used in other OS-based servers such as Microsoft's IIS or Netscape's Enterprise server. PHP can be downloaded freely at its homepage, http://www.php.net/.

PHP is highly similar to Microsoft's ASP (Active Server Pages) and Coldfusion. However, PHP is free and cross-platform. As a combination of C++ and CGI techniques, PHP can send HTTP headers, cookies, communicate flawlessly with databases, and much more. Unlike CGI, all PHP requires is a installation and your PHP-coded files to have the extension .php3 (it can also be configured to use .php, .phtml, and others.). To implement code, one would simply start as if you were writing a HTML file. To separate the coding from the HTML, you place the separators:


<?php
?>

(You can also configure PHP to use ASP-style tags (<% %>) or even <script LANGUAGE="php"></script>.)



To output from the code, one would use the command:
echo "Hello, $name";

The $name will automatically be parsed and printed. For instance, if $name was John, it would have printed:


Hello, John.
print $variable; can also be used, as well as printf("Hello, world!");



Since PHP is like C++ and CGI, statements MUST end with a semicolon, ;

echo "Hi!"

Doesn't work, while

echo "Hi!";

Does.

Hence, one would create a file named hello.php3 and put this code in it:

<html><head><title>PHP Test</title></head>
<body>
<?php
$name = "John";
echo "Hello $name\n";
?>
</body></html>

Comments are also used in PHP.


// This is a comment.
/* So is this! */
# Well, I am too!

Onto the next lesson!

Lesson II: Usefulness? Sure, why not!

Let's start with some interesting code.

<?php
if(strstr($HTTP_USER_AGENT,"MSIE")) {
echo "You are using Internet Explorer<br>";
}
?>

Wow, lots of new concepts to grasp today. We'll start with the if statement. The going for a if statement is as follows:


if ($name == $myname) { run_code; } // or...
if ($name == "John") { run_code; } // Else statments can be used too:
if ($name == "John") { echo "Hi John!"; } else { echo "Who are you?"; } // Don't forget elseif!
if ($name == "John") { echo "Hi John!"; } elseif ($name == "Suzy") { echo "Heya Suzy!"; } else { echo "Who are you?"; }



Notice that the == has two equals. == is comparative. To see if they are unequal, one would use !=
Just one equals sign, or =, is used for assigning values, not comparing.
Now that we've got that out of the way, let's move on.

if (strstr($HTTP_USER_AGENT,"MSIE")) {

Okay, strstr() is a PHP function that checks if one string includes another. $HTTP_USER_AGENT is a HTTP-sent variable. It is sent by the requesting browser of the visitor. So if $HTTP_USER_AGENT contains MSIE, the code will print out:


You are using Internet Explorer<br>


Of course, the <br> will be interpreted by the server and sent out as a newline.

Variables in PHP are dealt with very similarly to CGI and C++.

$var = 23;
$othervar = 12;
$var++; // This makes $var be 24.
$var = $var + 2; // $var now is 26. It can also be written as $var += 2;
$var = $var + $othervar; // $var is now 26 + 12, or 38.

Strings are dealt with a little differently.

$firstname = "John";
$lastname = "Doe";
$fullname = $firstname . $lastname; // A . is used combine strings.



$firstname .= " " . $lastname // Makes $firstname become "John Doe". Note the " " to add the space between the names.
echo "Hi, $firstname!\n"; // Outputs "Hi, John Doe!" with a newline in the code. Newlines are used to make code easier to read.



Next lesson!

Lesson III: Loops, loops, and more loops!

What is a loop? A loop is simply a structure of code that repeats itself until a certain point has been reached. Let's start with the while loop.

echo "Let's count to 10!";
// There's no need to set $x to 0, PHP assumes all unset variables to be 0.
while ($x < 11) { // < means less than, > means more than. You could also make this $x != 11
echo $x . " ";
$x++;
}
This would output: 1 2 3 4 5 6 7 8 9 10
Simple, eh? The code can also be structured like so:
do {
echo $x . " ";
$x++;
} while ($x < 11);

Same exact output, just the while statement is checked at the end of the code.

For loops are quite similar, and a little more complex.

for($x = 1; $x < 11; $x++) {
echo $x . " ";
}

On we go!



Lesson IV: Using Forms

Now that you've heard all this talk of variables and loops and the such, you're probably asking, "How in the heck is this going to help me?" Well, I'll show you. Say you had a php3 file named demo.php3 with the following form in it:

<?php
function displayform() { // functions are code that is run only when called.
global $PHP_SELF; // The script itself, demo.php3. To make the variable accessible in all functions, use global.
?>
<FORM METHOD="POST" ACTION="<?php echo $PHP_SELF; ?>"> // see how we made it act on itself?
Name: <INPUT TYPE="text" NAME="name">
<input type="submit" value="Submit"><input type="reset" value="Reset">
</FORM>
<?
}
function doform() {
global $name;
if ($name = "John Smith") { echo "Hey John!"; } else { echo "Who are you?"; }
}
if (empty($name)) {
displayform(); // If the variable wasn't set, make the form reappear.
} else {
doform(); // If it was, process the form.
}

Of course, there is many more practical usages and ways to code this. It's coded simply so you could easily understand. Next lesson!

Lesson V: Cookies!

Cookies, although often put down, are a very powerful part of web programming. Let's say you wanted to give a user a nickname, but you wanted it to store over a long period of time. No problem! Here's the code for cookie.php3

<?php
function addcookie() {
$nick = "Sammy"; // The value of the cookie
$var = "nick"; // The name of the cookie
setcookie($var,$nick,time()+360); // add the cookie, with the name "nick" and the value "Sammy", and make it last 5 minutes.
Header("Location: cookie.php3"); // Send the user back to this script. Note: Header must be used BEFORE any other output.
}
function docookie() {
$nickname = $HTTP_COOKIE_VARS["nick"]; // set the value of the cookie "nick" to $nickname
echo " Hi $nickname!";
}
if (!$HTTP_COOKIE_VARS["nick"]) { // Check to see if the cookie "nick" exists, if not, set it. If so, execute the code.
addcookie();
} else { docookie(); }
?>

The end.



Skrevet af: EMG| Dato : 2000-12-03 | Læst : 24837 gange | Print

Din kommentar og karakter til artiklen

Din karakter:

1

2

3

4

5

6

7

8

9

10

Kommentar:

Log ind for at give en karakter og kommentar.

Kommentarer og karakterer til artiklen

 


9/10

"Sådan skal det gøres. En fin engelsk guide, der beskriver funktionerne godt. Måske lidt flere eksempler til sidst. Du får 9"

PHP-Jonas | 06.09.2007, 07:27

 

Send Artikel/anmeldelse til: phpuni@phpuniverset.dk

Tilbage til oversigt

FORUM

Nyeste forum indlæg :

PHP/Opencart import module

Opencart/php import modul hjælp

Lækkert kontor / kontorfællesskab i Helsingør

Opdatering af PHP

Vurdering af hjemmeside

Nyeste forum svar :

Joomla som CMS

Android programmering

Grafik problemer

Prissammenligningsside

Jeg yder fremover ikke support!

 

MEDLEM

LOGIN ER IKKE MULIGT, MENS VI LAVER NYT SITE
 

NYHEDSBREV

Nyhedsbrev

Tilmeld dig vores nyhedsbrev og modtag nyheder om nye scripts og tips samt om mange andre nye tiltag på Phpuniverset.dk


 

© Copyright 2000 Propelcom Phpuniverset's politik om personlige oplysninger, artikler & Koder Phpuniverset er optimeret til Mozilla 1024 * 768 Phpuniverset version: 3.0 beta
Webhost : Ignesco.dk - android talk & help

Bund