Home > Article > Backend Development > What is the role of true in PHP functions?
In PHP, true is a Boolean value, usually used to indicate that a condition is true or a sign that an operation is successful. In a function, true can have many functions, such as indicating that a function executes successfully, returns the correct result, or satisfies a certain condition.
The following are several specific code examples to illustrate the role of true in PHP functions:
Indicates that the function execution is successful:
function login( $username, $password) { //Verify username and password if ($username === 'admin' && $password === '123456') { // login successful return true; } else { // Login failed return false; } } if (login('admin', '123456')) { echo 'Login successful'; } else { echo 'Username or password is wrong'; }
In the above example, the login function receives the username and password. If the entered username and password meet the conditions, it returns true to indicate successful login, otherwise it returns false to indicate failed login. After calling the login function, determine whether the login is successful based on the return value.
indicates that the conditions are met:
function isAdult($age) { if ($age >= 18) { return true; } else { return false; } } $age = 20; if (isAdult($age)) { echo 'You are of age'; } else { echo 'You are underage'; }
In this example, the isAdult function is used to determine whether a person is an adult. If the age is greater than or equal to 18, it returns true, indicating that he is an adult; otherwise, it returns false, indicating that he is underage. Output corresponding prompt information based on the return value.
In general, in PHP functions, true is usually used to indicate that the function executed successfully, satisfied the condition, or returned the correct result. By returning true or false, we can perform different operations based on conditional judgment in the code.
The above is the detailed content of What is the role of true in PHP functions?. For more information, please follow other related articles on the PHP Chinese website!