If, when you test the file in a browser, you see the HTML code rather than your greeting, it may be because you launched the page by dragging it to your browser. The URL should start with 'http://' NOT 'file://'
La prima pagina PHP
Creare un file con nome ciao.php nella directory principale del web server (DOCUMENT_ROOT) che abbia il seguente contenuto:
Example #1 Il nostro primo script PHP: ciao.php
<html>
<head>
<title>Test PHP</title>
</head>
<body>
<?php echo "Hello World!<p>"; ?>
</body>
</html>
Usa il tuo browser per accedere al file con il tuo URL del web server, finendo con il collegamento al file /hello.php. Quando si sviluppa localmente questo URL sarà simile a http://localhost/hello.php o http://127.0.0.1/hello.php ma questo dipende dalla configurazione del web server. Se tutto è configurato correttamente, questo file verrà analizzato da PHP e il seguente output sarà inviato al vostro browser:
<html> <head> <title>Test PHP</title> </head> <body> <p>Hello World</p> </body> </html>
Questo programma è molto semplice e sicuramente non era necessario fare ricorso a PHP per creare una pagina come quella. Tutto ciò che essa fa è visualizzare: Hello World! usando l'istruzione echo di PHP. Nota che il file non ha bisogno di essere eseguibile o speciale in alcun modo. Il server viene a sapere che questo file deve essere interpretato da PHP perchè si è utilizzata l'estensione ".php", per la quale il server è configurato per passarlo a PHP. Si pensi ad esso come ad un normale file HTML nel quale sono contenuti uno speciale set di tags che permettono di eseguire una moltitudine di cose interessanti.
Se si è provato questo esempio e non ha dato alcun output, o è apparso un pop-up che chiedeva se scaricare la pagina, o se è apparso il file come testo, probabilmente che il server su cui si stanno effettuando le prove non ha abilitato PHP o non è configurato opportunamente. Provare a chiedere al proprio amministratore di abilitarlo per voi usando il capitolo del manuale dedicato all'Installazione. Se si vogliono sviluppare in locale script PHP, leggere anche il capitolo sull'installazione per assicurarsi che tutto sia configurato correttamente. Assicurarsi che si accede al file via http con il server che fornisce l'output. Se si richiama il file semplicemente dal proprio file system, allora non sarà analizzato da PHP. Se i problemi persistono comunque, non esitare a utilizzare una delle tante opzioni del» supporto PHP.
L'obiettivo dell'esempio è quello di mostrare il formato speciale dei tag PHP. In questo esempio abbiamo usato <?php per indicare l'inizio di un tag PHP. Quindi abbiamo scritto l'istruzione PHP e abbiamo lasciato la modalità PHP usando il tag di chiusura, ?>. All'interno di un file HTML come questo si può entrare ed uscire dalla modalità PHP quante volte si desidera. Per ulteriori dettagli, leggere la sezione del manuale su sintassi di base di PHP.
Nota: Nota riguardo i Line Feed
I Line feed hanno poco significato in HTML, rimangono comunque una buona idea per rendere l'aspetto dell'HTML bello e pulito inserendo le line feeds al suo interno. Una linefeed che segue immediatamente dopo una chiusura ?> sarà rimossa da PHP. Questo può essere estremamente utile quando si inseriscono molti blocchi PHP o si includono file contenenti PHP che non dovrebbero avere dell'output. Allo stesso tempo può esserci un po' di confusione. È possibile inserire uno spazio dopo la chiusura ?> per forzare uno spazio e un line feed da stampare, o puoi inserire un line feed esplicito nell'ultimo echo/print dall'interno del blocco PHP.
Nota: Nota riguardo gli editor di testo
Esistomo molti editor di testo e Integrated Development Environment (IDE) che possono essere usati per creare, modificare e gestire file PHP. Una lista parziale di questi strumenti è disponibile qui: » Lista degli Editor PHP. Se si desidera suggerire un nuovo programma, visitare la pagina sopra e chiedere al curatore di aggiungerlo alla lista. Avere un editor con evidenziazione della sintassi può essere utile.
Nota: Nota riguardo i Word Processor
Word processor quali StarOffice Writer, Microsoft Word e Abiword non sono una buona scelta per modificare i file PHP. Se si volessero utilizzare comunque per questo script d'esempio, ci si deve assicurare che il file venga salvato come testo o PHP non sarà in grado di leggere ed eseguire lo script.
Nota: Nota riguardo Blocco Note di Windows
Se si scrive codice usando l'applicazione di Windows Blocco Note, occorre assicurarsi che i file vengano salvati con estensione .php. (Blocco Note aggiunge automaticamente l'estensione .txt ai file, a meno che non si intraprenda uno dei passi descritti di seguito.) Quando si salva il file e viene chiesto il nome da assegnargli,scrivere il nome fra virgolette (ad esempio: "ciao.php"). In alternativa, si può cliccare sul menu a tendina 'Documenti di Testo' nella finestra di 'Salvataggio' e cambiare l'impostazione in "Tutti i File". A quel punto si può inserire il nome del file, senza usare le virgolette.
Ora che si è creato con successo uno script funzionante in PHP, è il momento di creare il più famoso script PHP! Effettuare una chiamata alla funzione phpinfo() e si vedranno un sacco di informazioni utili sul proprio sistema e configurazioni come le variabili predefinite disponibili, i moduli PHP caricati, e le impostazioni della configurazione. Prendere un po' di tempo e rivedere queste importanti informazioni.
Example #2 Ricevere informazioni sul sistema da PHP
<?php phpinfo(); ?>

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++)
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()).
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).
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!
document_root variable is located in your web server configuration file
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.
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.
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.