PHP Doku:: Ihre erste PHP-erweiterte Seite - tutorial.firstpage.html

Verlauf / Chronik / History: (11) anzeigen

Sie sind hier:
Doku-StartseitePHP-HandbuchEinführungKleines TutorialIhre erste PHP-erweiterte Seite

Ein Service von Reinhard Neidl - Webprogrammierung.

Kleines Tutorial

<<Was brauche ich?

Nützliches>>

Ihre erste PHP-erweiterte Seite

Erstellen Sie eine Datei mit dem Namen hallo.php und speichern Sie sie im Root-Verzeichnis Ihres Webservers (DOCUMENT_ROOT) mit dem folgenden Inhalt:

Beispiel #1 Unser erstes PHP-Skript: hallo.php

<html>
 <head>
  <title>PHP-Test</title>
 </head>
 <body>
 <?php echo '<p>Hallo Welt</p>'?>
 </body>
</html>

Benutzen Sie Ihren Browser, um die Datei über den Webserver-URL aufzurufen. Der URL muss mit /hallo.php enden. Wenn Sie lokal entwickeln, sieht der URL z.B. so aus: http://localhost/hallo.php oder http://127.0.0.1/hallo.php - andere Adressen sind aber, abhängig vom Webserver, auch möglich. Wenn Sie alles korrekt installiert haben, wird die Datei von PHP geparst und Sie werden die folgende Ausgabe in Ihrem Browser sehen:

<html>
 <head>
  <title>PHP-Test</title>
 </head>
 <body>
 <p>Hallo Welt</p>
 </body>
</html>

Das Beispiel ist extrem einfach und natürlich brauchen Sie PHP nicht, um eine Seite wie diese zu erstellen. Denn es macht nicht mehr, als Hallo Welt mit der echo()-Anweisung von PHP auszugeben. Bitte beachten Sie, dass die Datei nicht ausführbar sein muss. Der Server erkennt anhand der Dateiendung ".php", dass sie durch PHP interpretiert werden muss. Stellen Sie sich eine normale HTML-Datei vor, die eine Menge von speziellen Tags enthält, mit denen Sie einige interessante Dinge tun können.

Wenn Sie dieses Beispiel ausprobiert haben und Sie aber keine Ausgabe erhalten haben oder zum Download aufgefordert worden sind oder die komplette Datei als Text erhalten haben, dann ist es sehr wahrscheinlich, dass auf Ihrem Server PHP nicht aktiviert oder falsch konfiguriert ist. Fragen Sie in diesem Fall Ihren Administrator und weisen Sie ihn auf das Installations-Kapitel hin. Wenn Sie lokal entwickeln, lesen Sie bitte das Installations-Kapitel, um festzustellen, ob alles richtig konfiguriert wurde. Stellen Sie sicher, dass Sie die datei über das HTTP-Protokoll aufrufen können. Wenn Sie die Datei direkt aus Ihrem Dateisystem aufrufen, wird sie nicht durch PHP geparst. Sollten Ihre Probleme nach Lesen dieses Kapitels immer noch bestehen, zögern Sie nicht und nutzen Sie eines der vielen » Support-Angebote.

Der wichtigste Punkt im Beispiel ist, Ihnen das spezielle PHP Tag-Format zu zeigen. Im Beispiel wurde <?php verwendet, um den Beginn eines PHP-Tags zu kennzeichnen. Anschließend folgte die PHP-Anweisung. Mit dem schließenden Tag, ?>, wurde der PHP-Modus wieder verlassen. Sie können an jeder Stelle und so oft Sie wollen, in den PHP-Modus wechseln und ihn wieder verlassen. Für weitere Details lesen Sie bitte den Abschnitt zu den Grundlagen der Syntax von PHP.

Hinweis: Anmerkungen zu Zeilenwechseln

Zeilenwechsel sind in HTML nur von geringer Bedeutung, trotzdem ist es sinnvoll HTML Code durch Zeilenwechsel zu formatieren um die Lesbarkeit zu erhöhen. Ein Zeilenwechsel der direkt auf ein schließendes ?>> folgt wird von PHP bei der Ausgabe entfernt. Dies ist äußerst nützlich wenn Sie viele PHP-Blöcke einfügen oder Dateien includieren die keine Ausgabe erzeugen sollen, auf der anderen Seite kann es aber auch verwirrend sein. Sie können einen Zeilenwechsel erzwingen indem sie entweder ein zusätliches Leerzeichen hinter ?> einfügen oder explizit mit echo oder print ein Zeilenwechselzeichen am ende ihres Codes ausgeben.

Hinweis: Anmerkung zu Text-Editoren

Es gibt eine ganze Reihe von Text-Editoren und Integrated Development Environments (IDEs), mit denen Sie Ihre PHP-Dateien erstellen, bearbeiten und managen können. Eine Liste solcher Programme finden Sie hier: » PHP Editors List. Wenn Sie einen Editor vorschlagen möchten, besuchen Sie bitte die genannte Seite und bitten Sie den Betreiber der Seite, dass er den Editor der Liste hinzufügt. Wir empfehlen Ihnen einen Editor zu benutzen, der Syntax-Highlighting bietet.

Hinweis: Anmerkung zu Textverarbeitungen

Textverarbeitungen wie StarOffice Writer, Microsoft Word und Abiword sind keine gute Wahl, um PHP-Dateien zu bearbeiten. Wenn Sie eines dieser Programme für dieses Test-Skript nutzen möchten, dann müssen Sie sicherstellen, dass die Datei als "Nur Text"-Datei gespeichert wird, da PHP sonst das Skript nicht lesen und nicht ausführen kann.

Hinweis: Anmerkung zu Notepad, dem Windows-Standard-Editor

Wenn Sie Ihre PHP-Skripte mit Notepad schreiben, müssen Sie sicherstellen, dass Ihre Dateien mit der Endung .php gespeichert werden. (Notepad fügt die Endung .txt automatisch an den Dateinamen an, wenn Sie das nicht mit einem der folgenden Schritte verhindern.) Wenn Sie die Datei speichern und einen Namen für die Datei eingeben sollen, dann setzen Sie den Dateinamen in Anführungszeichen (z.B. "hallo.php"). Alternativ können Sie auch im "Datei speichern"-Fenster in der Drop-Down-Liste "Dateityp" die Einstellung auf "Alle Dateien" ändern. Sie können dann den Dateinamen ohne Anführungszeichen eingeben.

Nachdem Sie jetzt erfolgreich ein einfaches, funktionierendes PHP-Skript geschrieben haben, wird es Zeit, das berühmteste PHP-Skript zu schreiben. Rufen Sie die Funktion phpinfo() auf und Sie bekommen viele nützliche Informationen über Ihr System und Ihre Installation wie z.B. die verfügbaren vordefinierten Variablen, die geladenen PHP-Module und die Konfigurations-Einstellungen. Nehmen Sie sich etwas Zeit und schauen Sie sich diese wichtigen Informationen an.

Beispiel #2 Hole Systeminformationen mit PHP

<?php phpinfo(); ?>


8 BenutzerBeiträge:
- Beiträge aktualisieren...
xiaomao5 at live dot com
3.09.2010 23:29
save your php script without the BOM when using UTF-8 or you will see "锘" in front of any output. A solution is save it without BOM(available in editplus and notepad++)
miklcct at gmail dot com
3.01.2010 14:56
If you save your code as UTF-8, make sure that the BOM (EF BB BF) is not present as the first 3 bytes of the file otherwise it may interfere with the code if the PHP need to be run before any output (e.g. header()).
jt at fuw dot edu dot pl
19.05.2007 13:48
Well, but PHP file ownership is important when server has safe_mode enabled - HTTP server checks it, uses it to set UID of process which executes it, or may even refuse to execute such a file - e.g. if one user is owner of main PHP file, and the main file includes another, owned by other user, this is considered to be security violation (quite reasonably).
HobbyTech
9.08.2006 7:12
On Windows, if file extensions can be hidden, you may not SEE that you have accidently saved a file as 'Text Documents' (and that the browser has added '.txt' to the end of your 'page.html', resulting in 'page.html.txt'.) You still see only 'page.html' even though it's really 'page.html.txt'. Also, if you try to rename it, it won't work because it's not overwriting the '.txt' part and not changing the filetype.

By the way, the hiding of file extensions is ALSO a way malicious crackers get you to click on an executable virus, fooling you into thinking it's an innocent document. You should always be able to view the extensions of all files on your system.

To view all extensions, open Windows Explorer. Click the 'Tools' menu, then 'Folder Options'. In the dialog box that appears, click the 'View' tab. In the 'Advanced Settings Box', scroll down to 'Hide extensions for known file types' and click the checkbox next to it to REMOVE THE CHECKMARK. Click the 'Apply to All Folders' button near the top of the dialog. This may or may not take a few minutes. Then click the 'OK' button to close the dialog.

Now, if something accidentally gets saved as the wrong filetype, resulting in another file extension automatically appended to the one you typed, you will see it and be able to rename it.

Of course, a badly-named file can be renamed simply by using 'Save As' and saving it as the proper filetype, but if you can't see the file extension, you may not know that is the problem. Also, renaming is easier than opening, resaving as a new filetype, and then deleting the old version!
c300501 at yahoo dot com
7.06.2006 15:26
document_root variable is  located in your web server configuration file
onebadscrivener at gmail dot com
18.01.2005 2:25
OS X users editing in TextEdit will need to make sure their TextEdit preferences are set to allow plain text files.  Under the TextEdit pull-down menu, choose PREFERENCES, then under NEW DOCUMENT ATTRIBUTES in the window that pops up, click PLAIN TEXT. 

Then, in the section of that same window called "saving," DESELECT "append .txt extension to plain text files."  This will allow you to save your files with a .php extension.

Then close the PREFERENCES window.  You're good to go.
Curtis
10.08.2004 23:47
Expansion on saving w/ notepad/wordpad: (tested on XP; but should work on 2000,NT, and 98)

You can associate the .php file extension w/ Windows w/o going into the registry.

Open up My Computer or MSIE in file mode. Go to folder options > File types tab. Now click new. Add the extension as PHP or php. If you can't find the PHP application in the dropdown list under advanced, just go OK, for now. At least the extension is in place.

Now, try and create a php file by using the directions from this page of the PHP tutorial (should save it with the rest of your HTML files, i.e. your DocumentRoot). If you go to view your php file listed in the directory, and you see that it's still a .txt file, right-click the icon to see if you can locate "open with." If so, you should be able to browse for the appropriate file, which should be at (may vary, depending on where you installed PHP):

C:\PHP\php.exe

Click that as the default program, and the PHP logo should appear on all your scripts, and no problems saving should occur w/ any program.

Good luck.
ryan420 at earthling dot net
3.02.2003 11:18
Note on permissions of php files:  You don't have to use 'chmod 0755' under UNIX or Linux; the permissions need not be set to executable.  Again, this is more like a html file than a cgi script.  The only mandatory requirement is that the web server process has read access to the php file(s).  With many Linux systems, it is popular for Apache to run under the 'apache' account.  Given that HTML and other web files, like php, are often owned by user 'root' and group 'web' (or another similar group name), acceptable permissions might be those achieved with 'chmod 664' or 'chmod 644'.  The web server process, running under the 'apache' account, will inherit read only permissions.  The 'apache' account is not root and is not a member of the 'web' group, so the "other" portion of the permissions (the last "4") applies.



PHP Powered Diese Seite bei php.net
The PHP manual text and comments are covered by the Creative Commons Attribution 3.0 License © the PHP Documentation Group - Impressum - mail("TO:Reinhard Neidl",...)