W3 Web Tutorials
PHP Tutorial

PHP Tutorial

PHP is a server scripting language, and a powerful tool for making dynamic and interactive Web pages.

PHP Introduction

PHP files can contain text, HTML, CSS, JavaScript, and PHP code. PHP code is executed on the server, and the result is returned to the browser as plain HTML.

Example

<!DOCTYPE html>
<html>
<body>

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

</body>
</html>

PHP Syntax

A PHP script starts with <?php and ends with ?>. PHP statements end with a semicolon.

Example

<?php
// This is a single-line comment
echo "Hello PHP";
?>

PHP Variables

In PHP, a variable starts with the $ sign, followed by the name of the variable.

Example

<?php
$txt = "Hello world!";
$x = 5;
$y = 10.5;
?>

PHP Echo

The echo statement is used to output data to the screen.

Example

<?php
echo "Welcome home!";
?>

PHP Form Handling

When a form is submitted, PHP can read the values from the $_POST or $_GET superglobal.

Example

<?php
$name = $_POST["name"];
$email = $_POST["email"];
echo "Welcome " . $name;
?>

PHP MySQL Connect

PHP can open a connection to a MySQL database with MySQLi. Always check that the connection succeeded.

Example

<?php
$servername = "localhost";
$username = "username";
$password = "password";

$conn = new mysqli($servername, $username, $password);

if ($conn->connect_error) {
  die("Connection failed: " . $conn->connect_error);
}
echo "Connected successfully";
?>

Example

<?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "mottl1";

// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);

// Check connection
if ($conn->connect_error) {
  die("Connection failed: " . $conn->connect_error);
}
?>

PHP MySQL Insert

After a connection is open, INSERT statements can add a new row to a table. A common pattern is to run the query only after the form is submitted.

Example

<?php
$sql = "INSERT INTO MyGuests (firstname, lastname, email)
VALUES ('John', 'Doe', 'john@example.com')";

if ($conn->query($sql) === TRUE) {
  echo "New record created successfully";
} else {
  echo "Error: " . $sql . "<br>" . $conn->error;
}
?>

Example

<?php
if (isset($_POST['submit'])) {
$jmeno=$_POST['name'];
$email=$_POST['email'];
$recenze=$_POST['comment'];
//pridat datum timestamp default zmenit na current

$sql = "INSERT INTO mottl1 (jmeno, email, recenze) VALUES ('$jmeno', '$email', '$recenze')";

if ($conn->query($sql) === TRUE) {
  //echo "New record created successfully";
header("location: index.php");
exit();
} else {
  echo "Error: " . $sql . "<br>" . $conn->error;
}
}
?>

PHP MySQL Select

The SELECT statement is used to select data from a database. The result is stored in a result object that you can loop through.

Example

<?php
$sql = "SELECT id, firstname, lastname FROM MyGuests";
$result = $conn->query($sql);

if ($result->num_rows > 0) {
  while($row = $result->fetch_assoc()) {
    echo "id: " . $row["id"]. " - Name: " . $row["firstname"]. "<br>";
  }
} else {
  echo "0 results";
}
$conn->close();
?>

Example

<?php
$sql = "SELECT id, jmeno, email, recenze, datum FROM mottl1";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
while($row = $result->fetch_assoc()) {
echo "id: " . $row["id"]. " - Name: " . $row["jmeno"]. " " . $row["email"]. " " . $row["recenze"]. " ". $row["datum"]. "<br>";
  }
} else {
    echo "0 results";
}

$conn->close();
?>

PHP If Statement

The if statement executes some code if one condition is true.

Example

<?php
$t = date("H");
if ($t < "20") {
  echo "Have a good day!";
}
?>