Basic PHP Syntax
- A PHP script or code starts with <?php and ends with ?>
- The default file extension for PHP files is “.php”. For example: file1.php
- A PHP file normally contains some PHP statements, variables, functions and some HTML tags as well.
Let us start with a simple PHP script. We will create a program to print “Hello World!” using the PHP function echo.
<?php echo "Hello World!"; ?>
Live Example: http://ideone.com/KZQp6x
How to run PHP Program:
- If you have installed XAMPP, all of your PHP code files will be placed in “htdocs” folder. The full path of htdocs folder will be something like D:\xampp\htdocs.
- Open notepad and create a simple program. You can copy the above example as well.
- Then save your file with PHP extension such as sample.php. You need to place or Save your ‘sample.php’ file in htdocs folder.
- Now you can open any web browser such as chrome and run your PHP file using the localhost URL.
- For example, if your file is located: ‘D:\xampp\htdocs\sample.php’, You need to type in browser URL: ‘http://localhost/sample.php’ and hit enter. You PHP program will run and give output such as ‘Hello World’
Common problems during first PHP program execution
When we run our first PHP program or script, it may have the following issues:
PHP is not running
Sometimes we install PHP/XAMP but do not start it. Make sure that your web server (Apache) is running and PHP is working. If you installed PHP using XAMPP/WAMP, you can hit the localhost URL (http://localhost/) to verify that PHP is running
File extension is not proper
Most of the time we use ‘notepad’ to create our first PHP program. In some Windows, when we save our file in notepad as PHP, it actually save it as .txt extension (text file). If you try to run PHP file, it will not be located and give error 404 (Page not found). Make sure your file is saved as ‘.php’ extensio
File location or Running URL/Path is not proper
Make sure you place file in htdocs folder and your are using the right localhost or server URL.
You first PHP program provides base for your learning. Many users simply do not continue, if they are not able to run their first program.
|Exercises & Assignments|
|Simple Tips for PHP Beginners|
|Experiment with Basic PHP Syntax Errors|
|Using phpinfo() – Display PHP Configuration & Modules|
|Interview Questions & Answers|
|What is the correct & common way to start and finish a PHP block of code?|
|What is the default extension of the PHP file?|