Sample codes in php

Your first PHP-enabled page

Create a file named hello.php and put it in your web server’s root directory ( DOCUMENT_ROOT ) with the following content:

Example #1 Our first PHP script: hello.php

Use your browser to access the file with your web server’s URL, ending with the /hello.php file reference. When developing locally this URL will be something like http://localhost/hello.php or http://127.0.0.1/hello.php but this depends on the web server’s configuration. If everything is configured correctly, this file will be parsed by PHP and the following output will be sent to your browser:

This program is extremely simple and you really did not need to use PHP to create a page like this. All it does is display: Hello World using the PHP echo statement. Note that the file does not need to be executable or special in any way. The server finds out that this file needs to be interpreted by PHP because you used the «.php» extension, which the server is configured to pass on to PHP. Think of this as a normal HTML file which happens to have a set of special tags available to you that do a lot of interesting things.

If you tried this example and it did not output anything, it prompted for download, or you see the whole file as text, chances are that the server you are on does not have PHP enabled, or is not configured properly. Ask your administrator to enable it for you using the Installation chapter of the manual. If you are developing locally, also read the installation chapter to make sure everything is configured properly. Make sure that you access the file via http with the server providing you the output. If you just call up the file from your file system, then it will not be parsed by PHP. If the problems persist anyway, do not hesitate to use one of the many » PHP support options.

The point of the example is to show the special PHP tag format. In this example we used . You may jump in and out of PHP mode in an HTML file like this anywhere you want. For more details, read the manual section on the basic PHP syntax.

Note: A Note on Line Feeds

Line feeds have little meaning in HTML, however it is still a good idea to make your HTML look nice and clean by putting line feeds in. A linefeed that follows immediately after a closing ?> will be removed by PHP. This can be extremely useful when you are putting in many blocks of PHP or include files containing PHP that aren’t supposed to output anything. At the same time it can be a bit confusing. You can put a space after the closing ?> to force a space and a line feed to be output, or you can put an explicit line feed in the last echo/print from within your PHP block.

Note: A Note on Text Editors

There are many text editors and Integrated Development Environments (IDEs) that you can use to create, edit and manage PHP files. A partial list of these tools is maintained at » PHP Editors List. If you wish to recommend an editor, please visit the above page and ask the page maintainer to add the editor to the list. Having an editor with syntax highlighting can be helpful.

Note: A Note on Word Processors

Word processors such as StarOffice Writer, Microsoft Word and Abiword are not optimal for editing PHP files. If you wish to use one for this test script, you must ensure that you save the file as plain text or PHP will not be able to read and execute the script.

Now that you have successfully created a working PHP script, it is time to create the most famous PHP script! Make a call to the phpinfo() function and you will see a lot of useful information about your system and setup such as available predefined variables, loaded PHP modules, and configuration settings. Take some time and review this important information.

Читайте также:  Горизонтальная линия в HTML

Example #2 Get system information from PHP

Источник

PHP Syntax

A PHP script is executed on the server, and the plain HTML result is sent back to the browser.

Basic PHP Syntax

A PHP script can be placed anywhere in the document.

The default file extension for PHP files is » .php «.

A PHP file normally contains HTML tags, and some PHP scripting code.

Below, we have an example of a simple PHP file, with a PHP script that uses a built-in PHP function » echo » to output the text «Hello World!» on a web page:

Example

My first PHP page

Note: PHP statements end with a semicolon ( ; ).

PHP Case Sensitivity

In PHP, keywords (e.g. if , else , while , echo , etc.), classes, functions, and user-defined functions are not case-sensitive.

In the example below, all three echo statements below are equal and legal:

Example

Note: However; all variable names are case-sensitive!

Look at the example below; only the first statement will display the value of the $color variable! This is because $color , $COLOR , and $coLOR are treated as three different variables:

Example

$color = «red»;
echo «My car is » . $color . «
«;
echo «My house is » . $COLOR . «
«;
echo «My boat is » . $coLOR . «
«;
?>

PHP Exercises

Unlock Full Access 50% off

COLOR PICKER

colorpicker

Join our Bootcamp!

Report Error

If you want to report an error, or if you want to make a suggestion, do not hesitate to send us an e-mail:

Thank You For Helping Us!

Your message has been sent to W3Schools.

Top Tutorials
Top References
Top Examples
Get Certified

W3Schools is optimized for learning and training. Examples might be simplified to improve reading and learning. Tutorials, references, and examples are constantly reviewed to avoid errors, but we cannot warrant full correctness of all content. While using W3Schools, you agree to have read and accepted our terms of use, cookie and privacy policy.

Источник

PHP Examples

The following PHP section contains a wide collection of PHP programming examples. The PHP examples are categorized based on the topics, including array, function, date, string, JSON, file system, and many more. Each example includes multiple approaches to solve the problem.

Читайте также:  Изображение ссылка при наведении css

PHP Examples

PHP Programming Tutorial
Recent articles on PHP
PHP Output & Multiple Choice Questions

Topics

  1. How to write PHP code in different ways?
  2. How to write comments in PHP?
  3. How to use Codeignitor (PHP)?
  4. How to echo HTML in PHP?
  5. How to do Error handling in PHP?
  6. How to show All Errors in PHP?
  7. How to Start and Stop a Timer in PHP?
  8. How to create default function parameter in PHP?
  9. How to check if mod_rewrite is enabled in PHP?
  10. How to do Web Scrapping in PHP Using Simple HTML DOM Parser?
  11. How to pass form variables from one page to another page in PHP?
  12. How to display logged in user information in PHP?
  13. How to find out where a function is defined using PHP?
  14. How to Get $_POST from multiple check-boxes?
  15. How to Secure hash and salt for PHP passwords?
  16. How to detect search engine bots with PHP?
  17. How to set PHP development environment in windows?
  18. How to turn off PHP Notices?
  19. How to use ‘
  1. How to insert a new item in an array on any position in PHP?
  2. How to append one array to another in PHP?
  3. How to delete an Element From an Array in PHP?
  4. How to print all the values of an array in PHP?
  5. How to perform Array Delete by Value Not Key in PHP?
  6. How to remove Array Element and do Re-Indexing in PHP?
  7. How to count all array elements in PHP?
  8. How to insert an item at the beginning of an array in PHP?
  9. How to check if two arrays contain the same elements?
  10. How to merge two arrays keeping original keys in PHP?
  11. How to find the maximum and the minimum in a PHP array?
  12. How to check a key exists in an array in PHP?
  13. How to find the second most frequent element in a PHP array?
  14. How to sort an array of objects by object fields in PHP?
  15. How to sort an array of strings in natural and standard orders?
  16. How to print the last value of an array without affecting the pointer?
  17. How to merge the first index of an array with the first index of the second array?
  18. How to create a string by joining the array elements using PHP?
  19. How to sort an Array of Associative Arrays by Value of a Given Key in PHP?
  20. How to make a leaderboard using PHP?
  21. How to check an array is multidimensional or not in PHP?
  22. How to create Multidimensional Associative Array in PHP?
  23. How to merge the duplicate value in multidimensional array in PHP?
  24. How to convert multidimensional array to XML file in PHP?
  25. How to search by multiple key => value in PHP array?
  26. How to search by key=>value in a multidimensional array in PHP?
  27. How to write a PHP program to find the Standard Deviation of an array?
  28. How to write PHP program to check for Anagram?
  1. How to pass PHP Variables by reference?
  2. How to format Phone Numbers in PHP?
  3. How to use php serialize() and unserialize() Function
  4. How to implement callback in PHP?
  5. How to merge two or more arrays using array_merge()?
  6. How to print an arithmetic progression series using inbuilt functions in PHP?
  7. How to prevent SQL Injection in PHP?
  8. How to extract the user name from the email ID using PHP?
  9. How to count rows in MySQL table in PHP?
  10. How to parse a CSV File in PHP?
  11. How to generate simple random password from a given string using PHP?
  12. How to upload images in MySQL using PHP PDO?
  13. How to check foreach Loop Key Value in PHP?
  14. How to properly Format a Number With Leading Zeros in PHP?
  15. How to get a File Extension in PHP?
  16. How to build a Grocery Store Web App using PHP with MySQL?
  17. How to delete text from file using preg_replace() function in PHP?
  1. How to get the current Date and Time in PHP?
  2. How to change the date format using PHP?
  3. How to convert DateTime to String using PHP?
  4. How to get Time Difference in Minutes in PHP?
  5. How to return all dates between two dates in an array in PHP?
  6. How to sort an array of dates in PHP?
  7. How to get the time of the last modification of the current page in PHP?
  8. How to convert a Date into Timestamp using PHP?
  9. How to add 24 hours to a unix timestamp in PHP?
  10. How to sort a multidimensional array by date element in PHP?
  11. How to convert timestamp to readable date/time in PHP?
  12. How to find number of week days between two dates?
  13. How to convert string to Date and DateTime in PHP?
  14. How to get last day of a month from date in PHP?
  1. How to change strings in an array to uppercase?
  2. How to convert first character of all the words uppercase using PHP?
  3. How to get the last character of a string in PHP?
  4. How to convert uppercase string to lowercase using PHP?
  5. How to extract Numbers From a String in PHP?
  6. How to replace String in PHP?
  7. How to Encrypt and Decrypt a PHP String?
  8. How to display string values within a table using PHP?
  9. How to write Multi-Line Strings in PHP?
  10. How to check if a String Contains a Substring in PHP?
  11. How to append a string in PHP?
  12. How to remove white spaces only beginning/end of a string using PHP?
  13. How to Remove Special Character from String in PHP?
  14. How to create a string by joining the array elements using PHP?
  15. How to prepend a string in PHP?
  16. How to replace a word inside a string in PHP?
  17. How to remove all white spaces from a string in PHP?
  18. How to count the number of words in a string in PHP?
  19. How to find number of characters in a string in PHP?
  20. How to get a substring between two strings in PHP?
  21. How to get a variable name as a string in PHP?
  22. How to remove occurrences of a specific character from end of a string in PHP?
  23. How to convert string to boolean in PHP?
  24. How to generate Random String Using PHP?
  25. How to generate a random, unique, alphanumeric string in PHP?
  26. How to remove new lines from string in PHP?
  27. How to insert string at specified position in PHP?
  28. How to check a string is a rotation of another string?

Источник

Оцените статью