PHP Snippet 1:
$login = check_login('user', 'pass');
if($login === true)
loginUser(); //login was successful, finalize or whatever
else
echo $login; //error message
PHP Snippet 2:
class exampleClass {
private $error_message = '';
function exampleFunction($argument) {
if($argument == 'lemons') {
return true;
} else {
$this->error_message = 'Argument was not lemons.';
return false;
}
}
function getErrorMessage() {
return $this->error_message;
}
}
$example_session = new exampleClass();
if( ! $example_session->exampleFunction('apples') )
echo $example_session->getErrorMessage();
PHP Snippet 3:
//Notice the '&' before the variable name.
function check_login($user, $pass, &$message) {
if(!isset($user) || $user == '') {
$message["status"] = false; //fail
$message["message"] ='Please enter a valid username';
}
elseif(!isset($pass) || $pass == '') {
$message["status"] = false; //fail
$message["message"]= 'Please enter a valid password';
}
elseif (isset($pass) || $pass != ''){
$message["status"] = true; //success
$message["message"]= 'Username and password are valid';
return true;
}
return false;//you can return from within the if condition if you wish
}
PHP Snippet 4:
/* $message variable will be assigned and populated in the function check_login() */
if(check_login($uname, $pword, $message = []) === true )
{
//Do your stuff here
//@message array is avaialble
echo message["message"];
}
else
{
//means false
echo message["message"];
}
PHP Snippet 5:
function check_login($user, $pass) {
if (empty($user)) {
throw new InvalidArgumentException('Please enter a valid username');
}
if (empty($pass)) {
throw new InvalidArgumentException('Please enter a valid password');
}
return true
}
// snip
try {
check_login($userValue, $passValue);
} catch (Exception $e) {
// an error occurred
echo $e->getMessage();
}
PHP Snippet 6:
function check_login($user, $pass) {
if(!isset($user) || $user == '') {
echo "Please enter a valid username";
return false;
}
else if(!isset($pass) || $pass == '') {
echo "Please enter a valid password";
return false;
}
else {
return true;
}
}
PHP Snippet 7:
function check_login($user, $pass) {
if(!isset($user) || $user == '') {
$var = "Please enter a valid username";
$var = array();
$var['msg'] = "Please enter a valid username";
$var['error'] = "false";
return $var;
}
else if(!isset($pass) || $pass == '') {
$var = array();
$var['msg'] = "Please enter a valid username";
$var['error'] = "false";
return $var;
}
else {
return true;
}
}
PHP Snippet 8:
echo $var['msg'];
PHP Snippet 9:
$var['error'] == 'false'
PHP Snippet 10:
class errorLog {
private $reports = array();
public function __construct() {
}
public function set($fn, $message) {
if(!array_key_exists($fn, $this->reports)) {
$this->reports[$fn] = $message;
return true;
}
return false;
}
public function get($fn) {
return ($this->reports[$fn] ?? null);
}
}
$errorLog = new errorLog;
PHP Snippet 11:
exampleFunction($value, $errorLog) {
if($value == "correct_value") {
return true;
} else {
$errorLog->add(__FUNCTION__, "Wrong value!");
}
return false;
}
PHP Snippet 12:
if(exampleFunction("incorrect_value", $errorLog)) {
// Success (function returned TRUE)
} else {
// Display error
echo $errorLog->get("exampleFunction");
}