PHP Function Reference


PHP 함수 목록 (PHP Function Reference)

Certainly! Here’s an extensive list of PHP functions along with brief explanations and examples:


String Functions

strlen()

Returns the length of a string.

$string = "Hello, world!";
$length = strlen($string); // $length will be 13

str_replace()

Replaces all occurrences of a search string with a replacement string in a string.

$string = "Hello, world!";
$new_string = str_replace("world", "PHP", $string); // $new_string will be "Hello, PHP!"

strpos()

Finds the position of the first occurrence of a substring in a string.

$string = "Hello, world!";
$pos = strpos($string, "world"); // $pos will be 7

substr()

Returns part of a string.

$string = "Hello, world!";
$substring = substr($string, 7); // $substring will be "world!"

strtolower()

Converts a string to lowercase.

$string = "Hello, WORLD!";
$lowercase = strtolower($string); // $lowercase will be "hello, world!"

strtoupper()

Converts a string to uppercase.

$string = "Hello, world!";
$uppercase = strtoupper($string); // $uppercase will be "HELLO, WORLD!"

trim()

Strips whitespace (or other characters) from the beginning and end of a string.

$string = "   Hello, world!   ";
$trimmed = trim($string); // $trimmed will be "Hello, world!"

implode()

Joins array elements with a string.

$array = array('Hello', 'world', '!');
$string = implode(' ', $array); // $string will be "Hello world !"

explode()

Splits a string by a string.

$string = "Hello, world!";
$array = explode(', ', $string); // $array will be ['Hello', 'world!']

Array Functions

array_push()

Pushes one or more elements onto the end of an array.

$array = ['apple', 'banana'];
array_push($array, 'cherry', 'date'); // $array will be ['apple', 'banana', 'cherry', 'date']

array_pop()

Pops the element off the end of the array.

$array = ['apple', 'banana', 'cherry'];
$last_element = array_pop($array); // $last_element will be 'cherry', $array will be ['apple', 'banana']

array_merge()

Merges one or more arrays into one array.

$array1 = ['apple', 'banana'];
$array2 = ['cherry', 'date'];
$merged_array = array_merge($array1, $array2); // $merged_array will be ['apple', 'banana', 'cherry', 'date']

array_key_exists()

Checks if the specified key exists in the array.

$array = ['a' => 1, 'b' => 2, 'c' => 3];
if (array_key_exists('b', $array)) {
    echo "Key 'b' exists!";
} else {
    echo "Key 'b' does not exist!";
}

count()

Counts all elements in an array.

$array = ['apple', 'banana', 'cherry'];
$count = count($array); // $count will be 3

sort()

Sorts an array.

$array = ['banana', 'apple', 'cherry'];
sort($array); // $array will be ['apple', 'banana', 'cherry']

array_search()

Searches an array for a given value and returns the corresponding key if successful.

$array = ['apple', 'banana', 'cherry'];
$key = array_search('banana', $array); // $key will be 1

array_map()

Applies a callback function to each element of an array.

$array = [1, 2, 3];
$new_array = array_map(function($item) {
    return $item * 2;
}, $array); // $new_array will be [2, 4, 6]

File System Functions

fopen()

Opens a file or URL.

$file = fopen("example.txt", "r") or die("Unable to open file!");

fwrite()

Writes to an open file.

$file = fopen("example.txt", "w") or die("Unable to open file!");
fwrite($file, "Hello, world!");
fclose($file);

fclose()

Closes an open file pointer.

$file = fopen("example.txt", "r") or die("Unable to open file!");
fclose($file);

file_get_contents()

Reads entire file into a string.

$content = file_get_contents("example.txt");
echo $content;

file_put_contents()

Write a string to a file.

$content = "Hello, world!";
file_put_contents("example.txt", $content);

is_file()

Checks whether the filename is a regular file.

$file = "example.txt";
if (is_file($file)) {
    echo "File exists!";
} else {
    echo "File does not exist!";
}

mkdir()

Makes a directory.

$dir = "new_directory";
mkdir($dir);

rmdir()

Removes a directory.

$dir = "directory_to_delete";
rmdir($dir);

Date and Time Functions

date()

Formats a local time/date.

echo date("Y-m-d H:i:s"); // Outputs current date and time

strtotime()

Parses any English textual datetime description into a Unix timestamp.

$timestamp = strtotime("next Monday");
echo date("Y-m-d", $timestamp); // Outputs the date of next Monday

time()

Returns the current time measured in the number of seconds since the Unix Epoch (January 1 1970 00:00:00 GMT).

echo time(); // Outputs current timestamp

mktime()

Returns the Unix timestamp for a date.

$timestamp = mktime(0, 0, 0, 7, 1, 2024); // July 1, 2024
echo date("Y-m-d", $timestamp); // Outputs "2024-07-01"

Database Functions

mysqli_connect()

Opens a new connection to the MySQL server.

$host = "localhost";
$username = "root";
$password = "";
$dbname = "my_database";
$conn = mysqli_connect($host, $username, $password, $dbname);

mysqli_query()

Performs a query on the database.

$sql = "SELECT * FROM users";
$result = mysqli_query($conn, $sql);

mysqli_fetch_assoc()

Fetches a result row as an associative array from a MySQL database.

$row = mysqli_fetch_assoc($result);
echo $row['username'];

mysqli_insert_id()

Returns the auto-generated id used in the last query.

mysqli_query($conn, "INSERT INTO users (username) VALUES ('John')");
echo mysqli_insert_id($conn);

mysqli_close()

Closes a previously opened database connection.

mysqli_close($conn);

Mathematical Functions

abs()

Returns the absolute value of a number.

echo abs(-4.2); // Outputs 4.2

round()

Rounds a floating-point number.

echo round(3.4); // Outputs 3

sqrt()

Returns the square root of a number.

echo sqrt(16); // Outputs 4

pow()

Returns base raised to the power of exp.

echo pow(2, 3); // Outputs 8 (2^3)

rand()

Generates a random integer.

echo rand(1, 10); // Outputs a random number between 1 and 10

min()

Finds the lowest value.

echo min(2, 5, 1); // Outputs 1

max()

Finds the highest value.

echo max(2, 5, 1); // Outputs 5

Miscellaneous Functions

isset()

Determine if a variable is set and is not NULL.

if (isset($_POST['submit'])) {
    // Process form data
}

unset()

Unsets a given variable.

$var = "Hello";
unset($var);

header()

Sends a raw HTTP header.

header("Location: https://example.com");
exit();

session_start()

Initializes a new session or resumes the existing session.

session_start();
$_SESSION['username'] = 'John';

File Upload and Download Functions

move_uploaded_file()

Moves an uploaded file to a new location.

$target_dir = "uploads/";
$target_file = $target_dir . basename($_FILES["file"]["name"]);
move_uploaded_file($_FILES["file"]["tmp_name"], $target_file);

file_exists()

Checks whether a file or directory exists.

$file = "example.txt";
if (file_exists($file)) {
    echo "File exists!";
} else {
    echo "File does not exist!";
}

filesize()

Gets the size of the file

.

$file = "example.txt";
echo filesize($file);

Cryptographic Functions

md5()

Calculates the MD5 hash of a string.

echo md5("Hello, world!");

sha1()

Calculates the SHA-1 hash of a string.

echo sha1("Hello, world!");

password_hash()

Creates a new password hash.

$password = "secret";
$hashed_password = password_hash($password, PASSWORD_DEFAULT);

password_verify()

Verifies that a password matches a hash.

$hashed_password = '$2y$10$xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx';
$password = "secret";
if (password_verify($password, $hashed_password)) {
    echo "Password is correct!";
} else {
    echo "Password is incorrect!";
}

Regular Expression Functions

preg_match()

Perform a regular expression match.

$string = "Hello, world!";
if (preg_match("/Hello/", $string)) {
    echo "Match found!";
}

preg_replace()

Perform a regular expression search and replace.

$string = "Hello, world!";
$new_string = preg_replace("/world/", "PHP", $string);
echo $new_string; // Outputs "Hello, PHP!"

These PHP functions cover a wide range of tasks from basic string manipulation to advanced database operations and security measures. Each function is designed to perform specific tasks efficiently, providing developers with powerful tools for web development.


Leave a Reply

Your email address will not be published. Required fields are marked *