w3resource logo


php exercises

PHP Basic - Exercises, Practice, Solution

Secondary Nav

PHP basic [31 exercises with solution]

1. Write a PHP script to get the PHP version and configuration information. Go to the editor
Click me to see the solution

2. Write a PHP script to display the following strings. Go to the editor
Sample String :
'Tomorrow I \'ll learn PHP global variables.'
'This is a bad command : del c:\\*.*'

Expected Output :
Tomorrow I 'll learn PHP global variables.
This is a bad command : del c:\*.* 
Click me to see the solution

3. $var = 'PHP Tutorial'. Put this variable into the title section, h3 tag and as an anchor text within an HTML document. Go to the editor
Sample Output :

PHP Tutorial

PHP, an acronym for Hypertext Preprocessor, is a widely-used open source general-purpose scripting language. It is a cross-platform, HTML embedded server-side scripting language and is especially suited for web development.
Click me to see the solution

4. Create a simple HTML form and accept the user name and display the name through PHP echo statement. Go to the editor

Sample output of the HTML form :

sample html form

Click me to see the solution

5. Write a PHP script to get the client IP address. Go to the editor
Click me to see the solution

6. Write a simple PHP browser detection script. Go to the editor
Sample Output : Your User-Agent is: Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/35.0.1916.114 Safari/537.36
Click me to see the solution

7. Write a PHP script to get the current file name. Go to the editor
Click me to see the solution

8. Write a PHP script, which will return the following components of the url 'http://www.w3resource.com/php-exercises/php-basic-exercises.php'. Go to the editor

List of components : Scheme, Host, Path
Expected Output :
Scheme : http
Host : www.w3resource.com
Path : /php-exercises/php-basic-exercises.php
Click me to see the solution

9. Write a PHP script, which changes the color of the first character of a word. Go to the editor
Sample string : PHP Tutorial
Expected Output :
PHP Tutorial
Click me to see the solution

10. Write a PHP script, to check whether the page is called from 'https' or 'http'. Go to the editor
Click me to see the solution

11. Write a PHP script to redirect a user to a different page . Go to the editor
Expected output : Redirect the user to http://www.w3resource.com/
Click me to see the solution

12. Write a simple PHP program to check that emails are valid. Go to the editor
Hints : Use FILTER_VALIDATE_EMAIL filter that validates value as an e-mail address.
Note : The PHP documentation does not say that FILTER_VALIDATE_EMAIL should pass the RFC5321.
Click me to see the solution

13. Write a e PHP script to display string, values within a table. Go to the editor
Note : Use HTML table elements into echo.
Expected Output :
php table
Click me to see the solution

14. Write a PHP script to display source code of a webpage (e.g. "http://www.example.com/"). Go to the editor
Click me to see the solution

15. Write a PHP script to get last modified information of a file. Go to the editor
Sample filename : php-basic-exercises.php
Sample Output : Last modified Monday, 09th June, 2014, 06:45am
Click me to see the solution

16. Write a PHP script to count lines in a file.
Note : Store a text file name into a variable and count the number of lines of text it has. Go to the editor
Click me to see the solution

17. Write a PHP script to print current PHP version. Go to the editor
Note : Do not use phpinfo() function.
Click me to see the solution

18. Write a PHP script to delay the program execution for the given number of seconds. Go to the editor
Click me to see the solution

19. Arithmetic operations on character variables : $d = 'A00'. Using this variable print the following numbers. Go to the editor
Sample Output :
A01
A02
A03
A04
A05
Click me to see the solution

20. Write a PHP script to get the last occurred error. Go to the editor
Click me to see the solution

21. Write a PHP function to test whether a number is greater than 30, 20 or 10 using ternary operator. Go to the editor
Click me to see the solution

22. Write a PHP script to get the full URL. Go to the editor
Click me to see the solution

23. Write a PHP script to compare the PHP version. Go to the editor
Note : Use version_compare() function and PHP_VERSION constant.
Click me to see the solution

24. Write a PHP script to get the name of the owner of the current PHP script. Go to the editor
Click me to see the solution

25. Write a PHP script to get the document root directory under which the current script is executing, as defined in the server's configuration file. Go to the editor
Click me to see the solution

26. Write a PHP script to get the information about the operating system PHP is running on. Go to the editor
Click me to see the solution

27. Write a PHP script to print out all the credits for PHP. Go to the editor
Click me to see the solution

28. Write a PHP script to get the directory path used for temporary files. Go to the editor
Click me to see the solution

29. Write a PHP script to get the names of the functions of a module. Go to the editor
Note : Find XML, JSON functions etc.
Click me to see the solution

30. Write a PHP script to get the time of the last modification of the current page Go to the editor
Click me to see the solution

31. Write a PHP script to get the time of the last modification of the current page Go to the editor
Click me to see the solution

PHP Practice online:

<?php
?>

Go Top

Next :

More to Come !

List of Exercises with Solutions :



Join our Question Answer community to learn and share your programming knowledge.

Help the community:

PHP: Loop

JavaScript: Delete duplicates in an array

Oracle: Round the number