It is very easy to create a PHP file on the server. Open your notepad and follow the below instruction.
We will be creating a PHP file and writing the sentence “Hello World” which is the most common text in learning the PHP code.
Php file uses the written code and displays the text “Hello World” in the browser when it is rendered.
Below process apply to creating a PHP file on the server / cpanel. I am assuming you already have access to your service or cpanel.
Follow the process to create a PHP file.
1) Go to your server or cpanel root folder. Which will be a public_html file.
2) Create a new file (don’t create a folder) and name the file “new.php”.
Note: Ignore the double inverted commas. The file name should be new.php.
3) Edit the file and write following code in the file.
<?php
echo "Hello World";
?>
4) Save the file.
5) Open the new tab in the browser and access your domain name followed by “/” and the file name. In this case, our file name is “new.php”.
Your browser web page will look like this.
www.yourdomain.com/new.php
6) When you access the file. You will see the PHP file representing the text you have type in the file which is “Hello World”.
7) Done. You have successfully created the first PHP file.
If you are getting PHP code error then please look for the typo mistake. There will be two reasons why you are not getting the expected code.
The most common reason is the typing mistake. Php is very sensitive codding language. If you type the wrong code in the php line then it will not perform in the browser.
Another reason is your server might not have active php. You can contact your server hosting company to confirm that.
Code explanation:
<?php
echo "Hello World";
?>
<?
– The beginning code of PHP is required for stating the computer that you have started the php line. Without this beginning PHP code, the file will not run perfectly on the server.
php
– The word php after the beginning code tells the computer that the file containing the PHP code.
echo
– The Echo is the statement instruct computer to execute the code mention after the word echo.
" "
– This is the most important part of the PHP code. All your text should be placed in between the two double inverted commas. If you forget to add the commas then PHP will show the error message.
;
– Php sentence closing tag. The closing tag tells the computer that you have finished writing the one line. The coding tag is necessary for completing the sentence. It works similar to the period symbol we use at the end of the sentence.
?>
– Php closing tag. The tag completes the PHP code and ends the running at this movement. By writing the PHP closing tag you inform the computer that you have done writing the code. File with the missing closing tag will show error in the browser. so do not forget to add the closing tag.