PHP Simple Code Hello World

Here's a simple PHP code snippet that prints "Hello, World!" on the screen:

?php
    echo "Hello, World!";
?>

Explanation

  • The <?php tag indicates the start of PHP code.
  • echo is a PHP statement used to output text or variables.
  • In this case, we're using echo to output the string "Hello, World!".
  • The code ends with the closing ?> tag.

How to run PHP Code in XAMPP

To run this code, just follow some simple step given below.

  1. Start Apache: In the XAMPP control panel, click the "Start" button next to the "Apache" module. This will start the Apache web server, which is needed to run PHP code.

  2. Save the file in the htdocs directory of your XAMPP installation. In Windows, the default path is C:\xampp\htdocs. On macOS, it is located in /Applications/XAMPP/htdocs. On Linux, the path may vary but is often /opt/lampp/htdocs.

  3. Access the PHP file: Open a web browser and enter the following URL: http://localhost/hello.php. Replace hello.php with the filename you used. The browser will request the file from the local web server (Apache), which will process the PHP code and display the output in the browser.

Make sure that Apache is running in the XAMPP control panel while accessing the PHP file. If you make changes to the PHP file, you'll need to refresh the browser to see the updated output.

That's it! You can now run and test PHP code using XAMPP on your local machine. XAMPP provides an environment that includes Apache, PHP, and MySQL, allowing you to develop and test dynamic web applications locally.

This is a basic example, but PHP can do much more. It supports variables, conditional statements, loops, functions, and interaction with databases, among other things. You can build complex applications and websites using PHP's wide range of features and libraries.

Next Article ❯