Home  >  Article  >  Backend Development  >  Definition and function introduction of PHP

Definition and function introduction of PHP

WBOY
WBOYOriginal
2024-03-27 17:00:05999browse

Definition and function introduction of PHP

Definition and function introduction of PHP

PHP is a server-side scripting language, originally developed in 1994 by Rasmus Lerdorf. Its initials originally meant "Personal Home Page" and are now interpreted as "Hypertext Preprocessor". This language is mainly used in the field of web development. PHP is used in conjunction with HTML to create dynamic pages, process form data, connect to databases and other functions. The following will introduce some basic functions of PHP and provide specific code examples.

  1. Variables and data types
    Variables in PHP start with the $ symbol, and there is no need to declare the data type in advance. PHP supports various data types, including integers, floating point numbers, strings, Boolean values, arrays, etc.

Example:

<?php
$name = "Alice";
$age = 25;
$height = 1.65;
$isStudent = true;
$fruits = array("apple", "banana", "orange");
?>
  1. Control flow
    PHP supports common control flow structures, such as if statements, for loops, while loops, etc.

Example:

<?php
$grade = 85;

if ($grade >= 60) {
    echo "及格";
} else {
    echo "不及格";
}

for ($i = 1; $i <= 5; $i++) {
    echo $i . " ";
}

$num = 0;
while ($num < 3) {
    echo $num . " ";
    $num++;
}
?>
  1. Function
    PHP allows defining and calling functions, which can accept parameters and return values.

Example:

<?php
function add($a, $b) {
    return $a + $b;
}

echo add(3, 5); // 输出8
?>
  1. Database connection
    PHP can connect to various databases, the most common one is MySQL. Through database connection, operations such as data query, insertion, update, and deletion can be performed.

Example:

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

// 创建连接
$conn = new mysqli($servername, $username, $password, $dbname);

// 检查连接
if ($conn->connect_error) {
    die("连接失败: " . $conn->connect_error);
}

// 查询数据库
$sql = "SELECT id, name, age FROM students";
$result = $conn->query($sql);

if ($result->num_rows > 0) {
    while($row = $result->fetch_assoc()) {
        echo "ID: " . $row["id"]. " - Name: " . $row["name"]. " - Age: " . $row["age"]. "<br>";
    }
} else {
    echo "0 结果";
}

$conn->close();
?>

The above is an introduction to some basic functions of PHP. As a powerful server-side scripting language, PHP is widely used in Web development. Hope the above content is helpful to you.

The above is the detailed content of Definition and function introduction of PHP. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn