How do i start php code?


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.

A PHP script starts with <?php and ends with ?>:

<?php
// PHP code goes here
?>

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

<!DOCTYPE html>
<html>
<body>

<h2>My first PHP page</h2>

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

</body>
</html>

Try it Yourself »

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

<!DOCTYPE html>
<html>
<body>

<?php
ECHO "Hello World!<br>";
echo "Hello World!<br>";
EcHo "Hello World!<br>";
?>

</body>
</html>

Try it Yourself »

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

<!DOCTYPE html>
<html>
<body>

<?php
$color = "red";
echo "My car is " . $color . "<br>";
echo "My house is " . $COLOR . "<br>";
echo "My boat is " . $coLOR . "<br>";
?>

</body>
</html>

Try it Yourself »


PHP Exercises



How do i start php code?

Hello and welcome to the start of codeofaninja.com’s series of web development articles!

Contents

  • 1 Overview
  • 2 Install XAMPP
  • 3 Run Your First PHP Script
    • 3.1 Go to XAMPP server directory
    • 3.2 Create hello.php
    • 3.3 Code Inside hello.php
    • 3.4 Open New Tab
    • 3.5 Load hello.php
    • 3.6 Output
  • 4 Manage MySQL with PhpMyAdmin
    • 4.1 Create a Database
    • 4.2 Create a Table
    • 4.3 Insert Data
    • 4.4 Useful Videos
  • 5 Run PHP Script with Database
    • 5.1 Go to XAMPP server directory
    • 5.2 Create read_one.php
    • 5.3 Code Inside read_one.php
    • 5.4 Open Your Browser
    • 5.5 Load read_one.php
    • 5.6 Output
  • 6 Online Resources
  • 7 What’s Next?
  • 8 Related Tutorials
  • 9 Some Notes
    • 9.1 Found An Issue?
    • 9.2 Subscribe to CodeOfaNinja
    • 9.3 Thank You!
    • 9.4 Share our tutorial

Overview

Setting up a development environment for PHP programming is easy. Download the code editor you prefer, I personally like atom.io text editor.

Next is to install XAMPP, the most popular PHP development environment. This package contains Apache, PHP & MariaDB or MySQL database applications.

Many people emailed me with a main question: Mike, how to run a PHP script? This post is my answer to you guys and to those people who will need this in the future.

In the following tutorial, we will learn how to install XAMPP, how to run a PHP script, manage database with PhpMyAdmin and run a sample PHP script that fetches a record from the database.

Install XAMPP

Go to this link and download XAMPP for your operating system. XAMPP is available for Windows, Linux or Mac.

Here’s a video about how you can install and use XAMPP.

Run Your First PHP Script

The following is an example about how to run a PHP script. What this program does is show a “Hello World!” text on the screen or webpage.

Go to XAMPP server directory

I’m using Windows, so my root server directory is “C:\xampp\htdocs\”.

Create hello.php

Create a file and name it “hello.php

Code Inside hello.php

Open hello.php and put the following code.

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

Open New Tab

Run it by opening a new tab in your browser

Load hello.php

On you browser window, type http://localhost/hello.php

Output

You should see the following output.

How do i start php code?

Great job, you just run a PHP script!

Manage MySQL with PhpMyAdmin

MySQL is an open-source relational database management system (RDBMS). MySQL is a popular choice of database for use in web applications.

phpMyAdmin is a free and open source tool written in PHP intended to handle the administration of MySQL with the use of a web browser. In the following examples, we will see how easy we can handle MySQL with PhpMyAdmin.

Create a Database

  1. Go to http://localhost/phpmyadmin/
  2. Click the “New” link on the upper left corner (under recent tables)
  3. Fill out the “Database Name” field with “my_first_database“.
  4. Click the “Create” button
How do i start php code?

Create a Table

  1. Click “my_first_database” on the left side of the screen
  2. On the “Create Table” section, fill out the Name with “products” and Number of Columns with “6
  3. Click “Go” button
How do i start php code?
  1. Fill out the fields with id, name, etc.
  2. Mimic everything in the following image
  3. Click the “Save” button
How do i start php code?

Insert Data

Click the “products” table.

How do i start php code?

Click the “Insert” tab.

How do i start php code?

Fill out the form, mimic the data on the following image. Click the “Go” button.

How do i start php code?

Great job! We now have a database, a table inside the database and a record inside the table.

How do i start php code?

Useful Videos

1. Create a database and import MySQL file.

2. Create a database and create table.

Run PHP Script with Database

In the following steps, we will run a PHP script that fetches one record from the MySQL database.

Go to XAMPP server directory

Go to your “C:\xampp\htdocs\” directory

Create read_one.php

Create a file and name it “read_one.php

Code Inside read_one.php

The numbers 1-8 in the following code are called “code comments”. It explains each part of our simple code below. Open read_one.php and put the following code.

<?php
// 1. database credentials
$host = "localhost";
$db_name = "my_first_database";
$username = "root";
$password = "";
 
// 2. connect to database
$con = new PDO("mysql:host={$host};dbname={$db_name}", $username, $password);
 
// 3. prepare select query
$query = "SELECT id, name, description, price FROM products WHERE id = ? LIMIT 0,1";
$stmt = $con->prepare( $query );
 
// 4. sample product ID
$product_id=1;
 
// 5. this is the first question mark in the query
$stmt->bindParam(1, $product_id);
 
// 6. execute our query
$stmt->execute();
 
// 7. store retrieved row to the 'row' variable
$row = $stmt->fetch(PDO::FETCH_ASSOC);
 
// 8. show data to user
echo "<div>Name: " . $row['name'] . "</div>";
echo "<div>Description: " . $row['description'] . "</div>";
echo "<div>Price: $" . $row['price'] . "</div>";
?>

Open Your Browser

Run it by opening you your browser

Load read_one.php

On you browser window, type http://localhost/read_one.php

Output

You should see the following output.

How do i start php code?

Awesome! You are now ready to learn more about web programming and development.

Online Resources

Here in codeofaninja.com, we want to simplify learning for you to actually build something. But it is also important for you to read and study more. The following are my suggestions where to learn more.

  • PHP Manual – official PHP documentation.
  • PHP The Right Way – easy-to-read, quick reference for PHP popular coding standards and more.
  • PHP Delusions – proper use of PHP programming language, proper use, with the focus on disproving various delusions and superstitions.
  • PHP Cheat Sheets – Variable comparison, arithmetic and testing cheat sheet.
  • PHP FAQs – Frequently asked PHP questions
  • PHP Security Cheat Sheets – prevent attacks like XSS, SQL Injection and more.
  • Cross-Site Scripting Attacks (XSS) – Another good resource to learn cross-site scripting (XSS) and prevent it.

You can always go back to the list above while you go along our series of web programming tutorials.

What’s Next?

Learn our JavaScript tutorial for beginners.

We listed all our high quality full-stack web development tutorials here: Click here.

Some Notes

Found An Issue?

If you found a problem with this code, please send us an email. Before you send an email, please read our our code of conduct. Our team's email address is [email protected]

Please be descriptive about your issue. Please provide the error messages, screenshots (or screen recording) and your test URL. Thanks!


Subscribe to CodeOfaNinja

Receive valuable web development tutorials to your email. Subscribe now for FREE!



Thank You!

Please share this post if you think this is a useful tutorial about how to run a PHP script. Thanks for learning here in codeofaninja.com!

How do I start PHP programming?

Step 1: First of all, open the Apache Friends website and download XAMPP for Windows, and install it. Step 2: Start the XAMPP Program Control Panel. Click on the “Start” button next to the “Apache” to start your Apache Web Server. Also, start “MySQL” if your PHP programs depend on a MySQL database to run.

How do I start and end a PHP code?

In PHP, statements are terminated by a semicolon (;) like C or Perl. The closing tag of a block of PHP code automatically implies a semicolon, there is no need to have a semicolon terminating the last line of a PHP block. <? php echo 'This is a test string'; ?>

How do I run a PHP file?

Executing PHP files ¶.
Tell PHP to execute a certain file. $ php my_script.php $ php -f my_script.php. ... .
Pass the PHP code to execute directly on the command line. $ php -r 'print_r(get_defined_constants());' ... .
Provide the PHP code to execute via standard input ( stdin )..