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.

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
$string = "Hello, world!";
$length = strlen($string); // $length will be 13
$string = "Hello, world!"; $length = strlen($string); // $length will be 13
$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.

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
$string = "Hello, world!";
$new_string = str_replace("world", "PHP", $string); // $new_string will be "Hello, PHP!"
$string = "Hello, world!"; $new_string = str_replace("world", "PHP", $string); // $new_string will be "Hello, PHP!"
$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.

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
$string = "Hello, world!";
$pos = strpos($string, "world"); // $pos will be 7
$string = "Hello, world!"; $pos = strpos($string, "world"); // $pos will be 7
$string = "Hello, world!";
$pos = strpos($string, "world"); // $pos will be 7

substr()

Returns part of a string.

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
$string = "Hello, world!";
$substring = substr($string, 7); // $substring will be "world!"
$string = "Hello, world!"; $substring = substr($string, 7); // $substring will be "world!"
$string = "Hello, world!";
$substring = substr($string, 7); // $substring will be "world!"

strtolower()

Converts a string to lowercase.

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
$string = "Hello, WORLD!";
$lowercase = strtolower($string); // $lowercase will be "hello, world!"
$string = "Hello, WORLD!"; $lowercase = strtolower($string); // $lowercase will be "hello, world!"
$string = "Hello, WORLD!";
$lowercase = strtolower($string); // $lowercase will be "hello, world!"

strtoupper()

Converts a string to uppercase.

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
$string = "Hello, world!";
$uppercase = strtoupper($string); // $uppercase will be "HELLO, WORLD!"
$string = "Hello, world!"; $uppercase = strtoupper($string); // $uppercase will be "HELLO, WORLD!"
$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.

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
$string = " Hello, world! ";
$trimmed = trim($string); // $trimmed will be "Hello, world!"
$string = " Hello, world! "; $trimmed = trim($string); // $trimmed will be "Hello, world!"
$string = "   Hello, world!   ";
$trimmed = trim($string); // $trimmed will be "Hello, world!"

implode()

Joins array elements with a string.

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
$array = array('Hello', 'world', '!');
$string = implode(' ', $array); // $string will be "Hello world !"
$array = array('Hello', 'world', '!'); $string = implode(' ', $array); // $string will be "Hello world !"
$array = array('Hello', 'world', '!');
$string = implode(' ', $array); // $string will be "Hello world !"

explode()

Splits a string by a string.

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
$string = "Hello, world!";
$array = explode(', ', $string); // $array will be ['Hello', 'world!']
$string = "Hello, world!"; $array = explode(', ', $string); // $array will be ['Hello', 'world!']
$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.

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
$array = ['apple', 'banana'];
array_push($array, 'cherry', 'date'); // $array will be ['apple', 'banana', 'cherry', 'date']
$array = ['apple', 'banana']; array_push($array, 'cherry', 'date'); // $array will be ['apple', 'banana', 'cherry', 'date']
$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.

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
$array = ['apple', 'banana', 'cherry'];
$last_element = array_pop($array); // $last_element will be 'cherry', $array will be ['apple', 'banana']
$array = ['apple', 'banana', 'cherry']; $last_element = array_pop($array); // $last_element will be 'cherry', $array will be ['apple', 'banana']
$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.

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
$array1 = ['apple', 'banana'];
$array2 = ['cherry', 'date'];
$merged_array = array_merge($array1, $array2); // $merged_array will be ['apple', 'banana', 'cherry', 'date']
$array1 = ['apple', 'banana']; $array2 = ['cherry', 'date']; $merged_array = array_merge($array1, $array2); // $merged_array will be ['apple', 'banana', 'cherry', 'date']
$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.

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
$array = ['a' => 1, 'b' => 2, 'c' => 3];
if (array_key_exists('b', $array)) {
echo "Key 'b' exists!";
} else {
echo "Key 'b' does not exist!";
}
$array = ['a' => 1, 'b' => 2, 'c' => 3]; if (array_key_exists('b', $array)) { echo "Key 'b' exists!"; } else { echo "Key 'b' does not exist!"; }
$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.

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
$array = ['apple', 'banana', 'cherry'];
$count = count($array); // $count will be 3
$array = ['apple', 'banana', 'cherry']; $count = count($array); // $count will be 3
$array = ['apple', 'banana', 'cherry'];
$count = count($array); // $count will be 3

sort()

Sorts an array.

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
$array = ['banana', 'apple', 'cherry'];
sort($array); // $array will be ['apple', 'banana', 'cherry']
$array = ['banana', 'apple', 'cherry']; sort($array); // $array will be ['apple', 'banana', 'cherry']
$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.

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
$array = ['apple', 'banana', 'cherry'];
$key = array_search('banana', $array); // $key will be 1
$array = ['apple', 'banana', 'cherry']; $key = array_search('banana', $array); // $key will be 1
$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.

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
$array = [1, 2, 3];
$new_array = array_map(function($item) {
return $item * 2;
}, $array); // $new_array will be [2, 4, 6]
$array = [1, 2, 3]; $new_array = array_map(function($item) { return $item * 2; }, $array); // $new_array will be [2, 4, 6]
$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.

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
$file = fopen("example.txt", "r") or die("Unable to open file!");
$file = fopen("example.txt", "r") or die("Unable to open file!");
$file = fopen("example.txt", "r") or die("Unable to open file!");

fwrite()

Writes to an open file.

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
$file = fopen("example.txt", "w") or die("Unable to open file!");
fwrite($file, "Hello, world!");
fclose($file);
$file = fopen("example.txt", "w") or die("Unable to open file!"); fwrite($file, "Hello, world!"); fclose($file);
$file = fopen("example.txt", "w") or die("Unable to open file!");
fwrite($file, "Hello, world!");
fclose($file);

fclose()

Closes an open file pointer.

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
$file = fopen("example.txt", "r") or die("Unable to open file!");
fclose($file);
$file = fopen("example.txt", "r") or die("Unable to open file!"); fclose($file);
$file = fopen("example.txt", "r") or die("Unable to open file!");
fclose($file);

file_get_contents()

Reads entire file into a string.

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
$content = file_get_contents("example.txt");
echo $content;
$content = file_get_contents("example.txt"); echo $content;
$content = file_get_contents("example.txt");
echo $content;

file_put_contents()

Write a string to a file.

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
$content = "Hello, world!";
file_put_contents("example.txt", $content);
$content = "Hello, world!"; file_put_contents("example.txt", $content);
$content = "Hello, world!";
file_put_contents("example.txt", $content);

is_file()

Checks whether the filename is a regular file.

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
$file = "example.txt";
if (is_file($file)) {
echo "File exists!";
} else {
echo "File does not exist!";
}
$file = "example.txt"; if (is_file($file)) { echo "File exists!"; } else { echo "File does not exist!"; }
$file = "example.txt";
if (is_file($file)) {
    echo "File exists!";
} else {
    echo "File does not exist!";
}

mkdir()

Makes a directory.

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
$dir = "new_directory";
mkdir($dir);
$dir = "new_directory"; mkdir($dir);
$dir = "new_directory";
mkdir($dir);

rmdir()

Removes a directory.

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
$dir = "directory_to_delete";
rmdir($dir);
$dir = "directory_to_delete"; rmdir($dir);
$dir = "directory_to_delete";
rmdir($dir);

Date and Time Functions

date()

Formats a local time/date.

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
echo date("Y-m-d H:i:s"); // Outputs current date and time
echo date("Y-m-d H:i:s"); // Outputs current date and time
echo date("Y-m-d H:i:s"); // Outputs current date and time

strtotime()

Parses any English textual datetime description into a Unix timestamp.

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
$timestamp = strtotime("next Monday");
echo date("Y-m-d", $timestamp); // Outputs the date of next Monday
$timestamp = strtotime("next Monday"); echo date("Y-m-d", $timestamp); // Outputs the date of next Monday
$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).

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
echo time(); // Outputs current timestamp
echo time(); // Outputs current timestamp
echo time(); // Outputs current timestamp

mktime()

Returns the Unix timestamp for a date.

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
$timestamp = mktime(0, 0, 0, 7, 1, 2024); // July 1, 2024
echo date("Y-m-d", $timestamp); // Outputs "2024-07-01"
$timestamp = mktime(0, 0, 0, 7, 1, 2024); // July 1, 2024 echo date("Y-m-d", $timestamp); // Outputs "2024-07-01"
$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.

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
$host = "localhost";
$username = "root";
$password = "";
$dbname = "my_database";
$conn = mysqli_connect($host, $username, $password, $dbname);
$host = "localhost"; $username = "root"; $password = ""; $dbname = "my_database"; $conn = mysqli_connect($host, $username, $password, $dbname);
$host = "localhost";
$username = "root";
$password = "";
$dbname = "my_database";
$conn = mysqli_connect($host, $username, $password, $dbname);

mysqli_query()

Performs a query on the database.

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
$sql = "SELECT * FROM users";
$result = mysqli_query($conn, $sql);
$sql = "SELECT * FROM users"; $result = mysqli_query($conn, $sql);
$sql = "SELECT * FROM users";
$result = mysqli_query($conn, $sql);

mysqli_fetch_assoc()

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

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
$row = mysqli_fetch_assoc($result);
echo $row['username'];
$row = mysqli_fetch_assoc($result); echo $row['username'];
$row = mysqli_fetch_assoc($result);
echo $row['username'];

mysqli_insert_id()

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

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
mysqli_query($conn, "INSERT INTO users (username) VALUES ('John')");
echo mysqli_insert_id($conn);
mysqli_query($conn, "INSERT INTO users (username) VALUES ('John')"); echo mysqli_insert_id($conn);
mysqli_query($conn, "INSERT INTO users (username) VALUES ('John')");
echo mysqli_insert_id($conn);

mysqli_close()

Closes a previously opened database connection.

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
mysqli_close($conn);
mysqli_close($conn);
mysqli_close($conn);

Mathematical Functions

abs()

Returns the absolute value of a number.

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
echo abs(-4.2); // Outputs 4.2
echo abs(-4.2); // Outputs 4.2
echo abs(-4.2); // Outputs 4.2

round()

Rounds a floating-point number.

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
echo round(3.4); // Outputs 3
echo round(3.4); // Outputs 3
echo round(3.4); // Outputs 3

sqrt()

Returns the square root of a number.

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
echo sqrt(16); // Outputs 4
echo sqrt(16); // Outputs 4
echo sqrt(16); // Outputs 4

pow()

Returns base raised to the power of exp.

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
echo pow(2, 3); // Outputs 8 (2^3)
echo pow(2, 3); // Outputs 8 (2^3)
echo pow(2, 3); // Outputs 8 (2^3)

rand()

Generates a random integer.

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
echo rand(1, 10); // Outputs a random number between 1 and 10
echo rand(1, 10); // Outputs a random number between 1 and 10
echo rand(1, 10); // Outputs a random number between 1 and 10

min()

Finds the lowest value.

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
echo min(2, 5, 1); // Outputs 1
echo min(2, 5, 1); // Outputs 1
echo min(2, 5, 1); // Outputs 1

max()

Finds the highest value.

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
echo max(2, 5, 1); // Outputs 5
echo max(2, 5, 1); // Outputs 5
echo max(2, 5, 1); // Outputs 5

Miscellaneous Functions

isset()

Determine if a variable is set and is not NULL.

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
if (isset($_POST['submit'])) {
// Process form data
}
if (isset($_POST['submit'])) { // Process form data }
if (isset($_POST['submit'])) {
    // Process form data
}

unset()

Unsets a given variable.

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
$var = "Hello";
unset($var);
$var = "Hello"; unset($var);
$var = "Hello";
unset($var);

header()

Sends a raw HTTP header.

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
header("Location: https://example.com");
exit();
header("Location: https://example.com"); exit();
header("Location: https://example.com");
exit();

session_start()

Initializes a new session or resumes the existing session.

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
session_start();
$_SESSION['username'] = 'John';
session_start(); $_SESSION['username'] = 'John';
session_start();
$_SESSION['username'] = 'John';

File Upload and Download Functions

move_uploaded_file()

Moves an uploaded file to a new location.

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
$target_dir = "uploads/";
$target_file = $target_dir . basename($_FILES["file"]["name"]);
move_uploaded_file($_FILES["file"]["tmp_name"], $target_file);
$target_dir = "uploads/"; $target_file = $target_dir . basename($_FILES["file"]["name"]); move_uploaded_file($_FILES["file"]["tmp_name"], $target_file);
$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.

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
$file = "example.txt";
if (file_exists($file)) {
echo "File exists!";
} else {
echo "File does not exist!";
}
$file = "example.txt"; if (file_exists($file)) { echo "File exists!"; } else { echo "File does not exist!"; }
$file = "example.txt";
if (file_exists($file)) {
    echo "File exists!";
} else {
    echo "File does not exist!";
}

filesize()

Gets the size of the file

.

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
$file = "example.txt";
echo filesize($file);
$file = "example.txt"; echo filesize($file);
$file = "example.txt";
echo filesize($file);

Cryptographic Functions

md5()

Calculates the MD5 hash of a string.

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
echo md5("Hello, world!");
echo md5("Hello, world!");
echo md5("Hello, world!");

sha1()

Calculates the SHA-1 hash of a string.

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
echo sha1("Hello, world!");
echo sha1("Hello, world!");
echo sha1("Hello, world!");

password_hash()

Creates a new password hash.

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
$password = "secret";
$hashed_password = password_hash($password, PASSWORD_DEFAULT);
$password = "secret"; $hashed_password = password_hash($password, PASSWORD_DEFAULT);
$password = "secret";
$hashed_password = password_hash($password, PASSWORD_DEFAULT);

password_verify()

Verifies that a password matches a hash.

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
$hashed_password = '$2y$10$xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx';
$password = "secret";
if (password_verify($password, $hashed_password)) {
echo "Password is correct!";
} else {
echo "Password is incorrect!";
}
$hashed_password = '$2y$10$xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx'; $password = "secret"; if (password_verify($password, $hashed_password)) { echo "Password is correct!"; } else { echo "Password is incorrect!"; }
$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.

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
$string = "Hello, world!";
if (preg_match("/Hello/", $string)) {
echo "Match found!";
}
$string = "Hello, world!"; if (preg_match("/Hello/", $string)) { echo "Match found!"; }
$string = "Hello, world!";
if (preg_match("/Hello/", $string)) {
    echo "Match found!";
}

preg_replace()

Perform a regular expression search and replace.

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
$string = "Hello, world!";
$new_string = preg_replace("/world/", "PHP", $string);
echo $new_string; // Outputs "Hello, PHP!"
$string = "Hello, world!"; $new_string = preg_replace("/world/", "PHP", $string); echo $new_string; // Outputs "Hello, PHP!"
$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 *