PHP programs
- A simple PHP program to print "Hello, world!
PHP
<?php
echo
"Hello, world!";
?>
- A PHP program to calculate the factorial of a number.
PHP
<?php
function
factorial(int $n) {
if ($n == 0) {
return 1;
} else {
return $n * factorial($n - 1);
}
}
echo
factorial(5); // 120
?>
- A PHP program to print the Fibonacci sequence.
PHP
<?php
function
fibonacci(int $n) {
if ($n == 0) {
return 0;
} else if ($n == 1) {
return 1;
} else {
return fibonacci($n - 1) + fibonacci($n
- 2);
}
}
for
($i = 0; $i <= 10; $i++) {
echo fibonacci($i) . " ";
}
?>
- A PHP program to check if a number is prime.
PHP
<?php
function
is_prime(int $n) {
if ($n < 2) {
return false;
} else if ($n == 2) {
return true;
} else {
for ($i = 2; $i < $n; $i++) {
if ($n % $i == 0) {
return false;
}
}
return true;
}
}
echo
is_prime(17) ? "true" : "false";
?>
- A PHP program to generate a random number.
PHP
<?php
function
random_number(int $min, int $max) {
return rand($min, $max);
}
echo
random_number(1, 100);
?>
- A PHP program to read and write a file.
PHP
<?php
$filename
= "myfile.txt";
$content
= "This is the content of my file.";
file_put_contents($filename,
$content);
$read_content
= file_get_contents($filename);
echo
$read_content;
?>
- A PHP program to connect to a MySQL database.
PHP
<?php
$host
= "localhost";
$dbname
= "mydb";
$username
= "root";
$password
= "";
$conn
= mysqli_connect($host, $username, $password, $dbname);
if
($conn) {
echo "Connection successful!";
}
else {
echo "Connection failed!";
}
?>
- A PHP program to insert a record into a MySQL table.
PHP
<?php
$host
= "localhost";
$dbname
= "mydb";
$username
= "root";
$password
= "";
$conn
= mysqli_connect($host, $username, $password, $dbname);
$sql
= "INSERT INTO users (name, email) VALUES ('John Doe',
'johndoe@example.com');";
$result
= mysqli_query($conn, $sql);
if
($result) {
echo "Record inserted
successfully!";
}
else {
echo "Error inserting record: " .
mysqli_error($conn);
}
?>
- A PHP program to select records from a MySQL table.
PHP
<?php
$host
= "localhost";
$dbname
= "mydb";
$username
= "root";
$password
= "";
$conn
= mysqli_connect($host, $username, $password, $dbname);
$sql
= "SELECT * FROM users";
$result
= mysqli_query($conn, $sql);
if
($result) {
while ($row = mysqli_fetch_assoc($result))
{
echo $row['name'] . " " . $row['email']
. "<br>";
}
}
else {
echo "Error selecting records: "
. mysqli_error($conn);
}
?
- A simple PHP program to print "Hello, world!
PHP
<?php
echo
"Hello, world!";
?>
- A PHP program to calculate the factorial of a number.
PHP
<?php
function
factorial(int $n) {
if ($n == 0) {
return 1;
} else {
return $n * factorial($n - 1);
}
}
echo
factorial(5); // 120
?>
- A PHP program to print the Fibonacci sequence.
PHP
<?php
function
fibonacci(int $n) {
if ($n == 0) {
return 0;
} else if ($n == 1) {
return 1;
} else {
return fibonacci($n - 1) + fibonacci($n
- 2);
}
}
for
($i = 0; $i <= 10; $i++) {
echo fibonacci($i) . " ";
}
?>
- A PHP program to check if a number is prime.
PHP
<?php
function
is_prime(int $n) {
if ($n < 2) {
return false;
} else if ($n == 2) {
return true;
} else {
for ($i = 2; $i < $n; $i++) {
if ($n % $i == 0) {
return false;
}
}
return true;
}
}
echo
is_prime(17) ? "true" : "false";
?>
- A PHP program to generate a random number.
PHP
<?php
function
random_number(int $min, int $max) {
return rand($min, $max);
}
echo
random_number(1, 100);
?>
- A PHP program to read and write a file.
PHP
<?php
$filename
= "myfile.txt";
$content
= "This is the content of my file.";
file_put_contents($filename,
$content);
$read_content
= file_get_contents($filename);
echo
$read_content;
?>
- A PHP program to connect to a MySQL database.
PHP
<?php
$host
= "localhost";
$dbname
= "mydb";
$username
= "root";
$password
= "";
$conn
= mysqli_connect($host, $username, $password, $dbname);
if
($conn) {
echo "Connection successful!";
}
else {
echo "Connection failed!";
}
?>
- A PHP program to insert a record into a MySQL table.
PHP
<?php
$host
= "localhost";
$dbname
= "mydb";
$username
= "root";
$password
= "";
$conn
= mysqli_connect($host, $username, $password, $dbname);
$sql
= "INSERT INTO users (name, email) VALUES ('John Doe',
'johndoe@example.com');";
$result
= mysqli_query($conn, $sql);
if
($result) {
echo "Record inserted
successfully!";
}
else {
echo "Error inserting record: " .
mysqli_error($conn);
}
?>
- A PHP program to select records from a MySQL table.
PHP
<?php
$host
= "localhost";
$dbname
= "mydb";
$username
= "root";
$password
= "";
$conn
= mysqli_connect($host, $username, $password, $dbname);
$sql
= "SELECT * FROM users";
$result
= mysqli_query($conn, $sql);
if
($result) {
while ($row = mysqli_fetch_assoc($result))
{
echo $row['name'] . " " . $row['email']
. "<br>";
}
}
else {
echo "Error selecting records: "
. mysqli_error($conn);
}
?
No comments:
Post a Comment