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://'
İlk PHP-etkin sayfanız
merhaba.php isimli bir dosya oluşturun ve HTML sunucunuzun belge kök dizinine (DOCUMENT_ROOT) aşağıdaki içerikle kaydedin:
Örnek 1 - İlk PHP betiğimiz: merhaba.php
<html>
<head>
<title>PHP Denemesi</title>
</head>
<body>
<?php echo '<p>Merhaba Dunya</p>'; ?>
</body>
</html>
Tarayıcınızı kullanarak HTTP sunucunuzun adresine erişebilirsiniz, sunucunuzun alan adının sonuna /merhaba.php eklemeniz yeterli olacaktır. Yerel olarak geliştirme yaparken bu URL http://localhost/merhaba.php ya da http://127.0.0.1/merhaba.php gibi bir şey olacaktır, ancak bu HTTP sunucusunun yapılandırma ayarlarına bağlıdır. Eğer her şey düzgün yapılandırılmışsa, bu dosya PHP tarafından işlenecek ve aşağıdaki çıktı tarayıcınıza gönderilecektir:
<html> <head> <title>PHP Denemesi</title> </head> <body> <p>Merhaba Dunya</p> </body> </html>
Bu program gördüğünüz gibi son derece basittir ve esasında böyle bir sayfa oluşturmak için PHP'ye gereksiniminiz yoktur. Görüntülenen: PHP'nin echo() deyimi kullanılarak elde edilen Merhaba Dünya cümlesidir. Bu dosyanın hiçbir şekilde çalıştırılabilir olmasına ya da özel bir türde olmasına ihtiyaç duyulmadığına dikkat edin. Sunucu dosyanın PHP tarafından işlenmesi gerektiğini bilir çünkü dosyaya ".php" uzantısını tanımladınız. Sunucu bu uzantıya sahip dosyaları PHP'ye aktaracak biçimde yapılandırılmıştır. Bunu size birtakım özel etiketlerle birçok ilginç işlem yapmanızı sağlayan bir tür HTML dosyası olarak düşünebilirsiniz.
Bu örneği denediyseniz ve hiçbir çıktı alamadıysanız, sizden dosyayı indirmenizi istediyse ya da dosyanın tüm içeriğini metin olarak ekranda gördüyseniz, büyük ihtimalle sunucunuzun PHP desteği etkinleştirilmemiştir ya da sunucunuz doğru yapılandırılmamıştır. Yöneticinizden kılavuzdaki Kurulum bölümünü kullanarak PHP'yi etkinleştirmesini rica edebilirsiniz. Yerel olarak geliştirme yapıyorsanız, kurulum bölümünü okuyarak herşeyin doğru yapılandırıldığından emin olun. Size çıktıyı sağlayan sunucudaki dosyaya http üzerinden erişmeye çalışıp çalışmadığınızı kontrol edin. Dosyayı doğrudan kendi dosya sisteminizden çağırıyorsanız, PHP tarafından işlenmeyecektir. Sorun her şeye rağmen devam ediyorsa, » PHP desteği alma yöntemlerini kullanmakta tereddüt etmeyin.
Bu örneğin amacı özel PHP etiketi biçemini göstermektir. Örnekte <?php ile PHP etiketinin başlangıcını belirledik. Sonra PHP komutunu yazdık ve kapanış etiketi ?> kullanarak PHP kipinden çıktık. Bir HTML dosyasında bu yöntemi kullanarak istediğiniz yerde PHP kipine girip çıkabilirsiniz. Daha fazla bilgi için, kılavuzun temel PHP sözdimi bölümünü okuyabilirsiniz.
Bilginize: Satırsonları hakkında
Satırsonu karakterlerinin HTML içinde bir önemi yoktur, bununla birlikte HTML kodunun göze hoş görünmesi için satırsonu karakterlerini kullanmak gerekir. ?> kapama etiketinin hemen ardından gelen satırsonu karakterini PHP silecektir. Dosyanın içine çok sayıda PHP bloğu koyarsanız veya hiçbir şey çıktılamayan betikleri dosyaya dahil etmeniz durumunda bu çok işe yarar. Ancak bu biraz kafa karıştırıcı olabilir. ?> kapama etiketinden sonra bir satırsonu karakterinin çıktılanmasını zorlamak isterseniz, ya kapama etiketinden sonra bir boşluk bırakıp onun ardına satırsonu karakterini koyun ya da PHP bloğunun son echo/print deyiminin sonuna bir satır sonu karakteri ekleyin.
Bilginize: Metin Düzenleyiciler Hakkında
PHP dosyalarını yaratabileceğiniz, düzenleyebileceğiniz ve yönetebileceğiniz birçok metin düzenleyici ve Bütünleşik Geliştirme Ortamı (IDE) bulunmaktadır. Bu araçların bir kısmı » PHP Düzenleyicileri Listesi bölümünde listelenmiştir. Sizin önermek istediğiniz bir düzenleyici varsa, lütfen yukarıdaki sayfayı ziyaret edin ve sayfa idarecisinden önermek istediğiniz düzenleyiciyi bu listeye eklemesini rica edin. Söz dizimi vurgulaması olan bir düzenleyici size yardımcı olabilir.
Bilginize: Kelime İşlemciler Hakkında
StarOffice Writer, Microsoft Word ve Abiword gibi kelime işlemciler PHP dosyalarını düzenlemek için uygun değillerdir. Eğer bu deneme betiği için bunlardan birini kullanmak istiyorsanız, dosyanın salt metin olarak kaydedildiğinden emin olun, aksi takdirde PHP'nin betiği okuması ve çalıştırması mümkün olmayacaktır.
Bilginize: Windows Not Defteri Hakkında
PHP betiklerinizi Windows Not Defteri ile yazıyorsanız, dosyalarınızın .php uzantısı ile kaydedildiğinden emin olmalısınız (Notepad birazdan bahsedeceğimiz önlemleri almadığınız takdirde dosyaların sonuna özdevinimli olarak .txt uzantısı ekler). Dosyayı kaydetmek istediğinizde, sizden dosyanız için bir isim belirtmeniz istenecektir. Dosya ismini tırnak içinde (örn. "hello.php") yazın. Bundan başka, 'Kaydet' iletişim kutusunun içindeki 'Metin Belgeleri' açılır menüsüne tıkladıktan sonra buradaki değeri "Tüm Dosyalar" olarak değiştirin. Artık dosya isminizi tırnak işareti kullanmadan yazabilirsiniz.
Artık çalışır bir PHP betiğini başarıyla oluşturduğunza göre, PHP betiklerinin en ünlüsünü oluşturma zamanı geldi demektir! phpinfo() işlevini çalıştıracağız ve sisteminiz ve yapılandırmanızla ilgili öntanımlı değişkenler, yüklü PHP modülleri ve yapılandırma ayarları gibi konularda faydalı birçok bilgiye ulaşacağız. Bu önemli bilgiler için biraz vakit ayırmanızı ve bunları incelemenizi öneririz.
Örnek 2 - PHP ile sistem bilgisine ulaşmak
<?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.
