A PHP script is executed on the server, and the plain HTML result is sent back to the browser. Show
Basic PHP SyntaxA PHP script can be placed anywhere in the document. A PHP script starts with <?php The default file extension for PHP files is " 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 " Example<!DOCTYPE html> <h2>My first PHP page</h2> <?php </body> Try it Yourself » Note: PHP statements end with a semicolon ( PHP Case SensitivityIn PHP, keywords (e.g. In the example below, all three echo statements below are equal and legal: Example<!DOCTYPE html> <?php </body> 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 Example<!DOCTYPE html> <?php </body> Try it Yourself » PHP ExercisesHello and welcome to the start of codeofaninja.com’s series of web development articles! Contents
OverviewSetting 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 XAMPPGo 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 ScriptThe 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 directoryI’m using Windows, so my root server directory is “C:\xampp\htdocs\”. Create hello.phpCreate a file and name it “ Code Inside hello.phpOpen <?php echo "Hello World!"; ?> Open New TabRun it by opening a new tab in your browser Load hello.phpOn you browser window, type OutputYou should see the following
output. Great job, you just run a PHP script! Manage MySQL with PhpMyAdminMySQL 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
Create a Table
Insert DataClick the “products” table. Click the “Insert” tab. Fill out the form, mimic the data on the following image. Click the “Go” button. Great job! We now have a database, a table inside the database and a record inside the table. Useful Videos1. Create a database and import MySQL file.
2. Create a database and create table.
Run PHP Script with DatabaseIn the following steps, we will run a PHP script that fetches one record from the MySQL database. Go to XAMPP server directoryGo to your “C:\xampp\htdocs\” directory Create read_one.phpCreate a file and name it “ Code Inside read_one.phpThe numbers 1-8 in the following code are called “code comments”. It explains each part of our simple code below. Open <?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 BrowserRun it by opening you your browser Load read_one.phpOn you browser window, type http://localhost/read_one.php OutputYou should see the following
output. Awesome! You are now ready to learn more about web programming and development. Online ResourcesHere 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.
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 NotesFound 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 CodeOfaNinjaReceive 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 ).. |