PHP Introduction

PHP is a widely-used, open-source server-side scripting language for building dynamic web pages.

Syntax<?php // your code here ?>

PHP stands for PHP: Hypertext Preprocessor. It is a server-side language, which means the code runs on the web server and only the result (usually HTML) is sent to the browser.

What can PHP do?

  • Generate dynamic page content.
  • Collect form data and process it.
  • Read, write and manage files on the server.
  • Talk to databases to store and fetch information.

PHP files have the extension .php and can mix PHP code with plain text and HTML.

Example

Try it yourself
Loading editor…
Press Run to execute the code.

When to use it

  • Building dynamic web pages that change based on data or the logged-in user.
  • Handling HTML form submissions and saving them to a database.
  • Powering REST APIs and backends for JavaScript front-ends.

More examples

Your first script

Code between <?php ?> runs on the server; everything else is sent to the browser as-is.

Example · php
<?php
echo "Hello, World!";

PHP mixed into HTML

You can drop small PHP blocks anywhere inside an HTML page.

Example · php
<h1><?php echo "Welcome"; ?></h1>
<p>Static HTML here.</p>

Show runtime info

Built-in functions like phpversion() and date() return information at run time.

Example · php
<?php
echo "PHP " . phpversion() . "\n";
echo "Today: " . date("Y-m-d");

Discussion

  • Be the first to comment on this lesson.