"Welcome to our PHP 101 blog, where we demystify the world of web development. "

Friday, January 19, 2024

Run PHP

 How to run PHP Code

Syntax to write PHP Programs and where is to write the PHP programs:

  1. PHP programs can be written on any editor, such as - Notepad, Notepad++,Visual Studio Code , Dreamweaver, etc. The programs you have to save with .php extension, i.e., filename.php inside the htdocs folder.
  2. Generally, a PHP file contains HTML tags and some PHP scripting code. To do so, create a file and write HTML tags + PHP code and save this file with .php extension.
  3. PHP statements ends with semicolon(;).
  4. All PHP code goes between the php tag. It starts with <?php and ends with ?>.
  5. As I'm using window, and my XAMPP server is installed in D drive. So, the path for the htdocs directory will be "D:\xampp\htdocs".
  6. PHP program runs on a web browser such as - Chrome, Internet Explorer, Firefox, etc.


Types of Running the PHP Programs:

PHP has two main types to run the PHP programs:

  1. Command-line scripting – like Python and Perl, you can run PHP script from the command line to perform administrative tasks like sending emails and generating PDF files.
  2. Server-side scripting – PHP is well-suited for developing dynamic websites and web applications.

I)  Command-line scripting

(How to run PHP programs in CLI):

Here are the steps to execute a PHP script from the command line:

Step 1: Open a Terminal or Command Prompt:

    On Windows, you can use Command Prompt or PowerShell. On Unix-like systems (Linux, macOS), you can use the terminal.

Step 2: Navigate to the Directory Containing Your PHP File:

    Use the cd command to navigate to the directory where your PHP file is located.
    Syntax :cd path/to/your/php/files

Step 3: Run the PHP Script:

     Once you are in the correct directory, use the php command followed by the name of your PHP file to execute it.
     syntax: phpyourfile.php
     Example: phphello.php

Step 4: View the Output:

     After executing the command, you should see the output of your PHP script in the terminal.
     Output: Hello, World!

Step 5: Remember that any PHP errors or output will be displayed in the terminal. 

If your PHP script interacts with the user through echo or print statements, you'll see the results directly in the CLI.

Example:
PHP script (hello.php) :
     <?php
       echo "Hello, World!";
      ?>
Save this code in a file named hello.php and then run it using the command mentioned earlier.
Code to compile php programs:
Example: php hello.php
Note: Ensure that PHP is installed on your system and added to the system's PATH variable. You can check the PHP version and availability by running php -v in the command line. If PHP is not recognized, you might need to install it or adjust your system's PATH settings

II)Server-side scripting-

(How to run PHP programs in XAMPP)

  1. How to run PHP programs in XAMPP PHP is a popular backend programming language. PHP programs can be written on any editor, such as - Notepad, Notepad++, Visual Studio Code , Dreamweaver, etc. These programs save with .php extension, i.e., filename.php inside the htdocs folder.
  2. As I'm using window, and my XAMPP server is installed in D drive. So, the path for the htdocs directory will be "D:\xampp\htdocs".
  3. PHP program runs on a web browser such as - Chrome, Internet Explorer, Firefox, etc. 

Step 1: Create a simple PHP program like hello world.

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

Step 2: Save the file 

        Save the file with p1.php name in the htdocs folder, which resides inside the xampp folder.

Step 3: Run the XAMPP server and start the Apache and MySQL.

Step 4: Now run your PHP program on server

Now, open the web browser and type localhost http://localhost/p1.php on your browser window.


Exercise: Run the phpinfo() Script


What is phpinfo()?
The phpinfo() function outputs detailed information about the PHP installation on your server. It includes data about the PHP version, the loaded extensions, and server environment settings.

Create a PHP File:
Create a new file in your htdocs folder named info.php.
Write the Script:
Add the following code:

<?php
    phpinfo();
?>
Run the Script:
Go to http://localhost/info.php in your browser.
This page will display all the configuration details of your PHP environment, confirming that PHP is correctly installed and running.





No comments:

Post a Comment

Pages

SoraTemplates

Best Free and Premium Blogger Templates Provider.

Buy This Template