PHP OPERATORS - PHP LOGICAL OPERATORS
The logical operators in PHP are:
- the and operator:
$a$ and $b$ returns TRUE when both $a$ and $b$ are TRUE
$a$ and $b$ can also be written $a && $b; the two expressions are almost equivalent, the only difference being that && has a higher precedence than most usual assignment and comparison operators; for instance, have a look at the PHP code below:
Learn the PHP code:
<?php
$a = true && false;
if ($a == false)
echo '$a is FALSE';
else
echo '$a is TRUE';
?> |
Run the PHP script in your web browser:
Remark:
In the above PHP code, the expression $a = true && false is equivalent to the expression $a = (true && false) because the operator && has higher precedence than the operator =. Thus in this case the variable $a will be FALSE.
This is not the case for the operator and, so that the expression $a = true and false will be interpreted as ($a = true) and false. Thus in this case the variable $a will be TRUE. You can verify this by executing the modified PHP code.
- the or operator:
$a$ or $b$ returns TRUE when either $a$ or $b$ is TRUE
$a$ or $b$ can also be written $a || $b; here again, the two expressions are rouhgly equivalent, the only difference being that && has a higher precedence.
- the xor operator:
$a$ xor $b$ returns TRUE when either $a$ or $b$ is TRUE, but not both.
- the not operator, written !:
!$a returns TRUE when $a is FALSE (i.e. when $a is not TRUE).
PHP operators are very much like those you have probably already met in other programming languages; the only point that demands special attention is that the equal operator in PHP is written == and not = (which is the PHP assignment operator).
I. MySQL use case
Consider the following MySQL tables :
CREATE TABLE `clients` ( `id` INT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY , `name` VARCHAR( 100 ) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL ) ENGINE = MYISAM |
CREATE TABLE `calls` ( `client_from` int(10) unsigned NOT NULL, `client_to` int(10) unsigned NOT NULL, `duration` int(10) unsigned NOT NULL, `cost` decimal(6,1) unsigned NOT NULL, `date` timestamp NOT NULL default '0000-00-00 00:00:00' ) ENGINE=MyISAM DEFAULT CHARSET=latin1
|
|
The « clients » table stores the different clients of a company providing mobile phone services.
Each client has a unique identifier (the « id » field) and other information (only a name to keep it simple).
The « calls » table stores each call between clients of the company. The « client_from » is the id of the calling client and client_to is the id of the callee. These ids exist in the clients table. Duration is stored in seconds and cost in ruppees. The data field store the starting date of the call.
- All the SQL statements must be written as single query and for a MySQL 5 database.
- Answers are not unique. There exist several manners to get same results. Please give the most optimized choice and/or the most simple way if you can. Please privilegiate solutions using EXPLICIT JOIN synthax (INNER JOIN, LEFT JOIN …)
- If you don’t know how to answer or have any doubts, please answer by explaining your misunderstanding or issues faced.
Please answer the following questions:
- Write a SQL statement to only get the « name » of all the company’s client.
Answer: SELECT `name` FROM `clients` ;
- Write a SQL statement adding the « Mittal » client to the « clients »’s table
Answer: INSERT INTO `clients`(`name`) VALUES('Mittal');
- Write a SQL statement to get the name and id of the clients who called at least one time. There must be unique results. Please give two possible answers if possible.
Answer :
1. SELECT a.`name`, a.`id` from `clients` a JOIN `calls` b on a.`id` = b.`client_from` group by a.`id`;
2. SELECT `name`, `id` from `clients` WHERE `id` IN (SELECT `client_from` FROM `calls` GROUP BY `client_from`);
- Write a SQL statement to get the name and id of the clients who received at least one call. There must be unique results. Please give two possible answers if possible.
Answer :
1. SELECT a.`name`, a.`id` from `clients` a JOIN `calls` b on a.`id` = b.`client_to` group by a.`id`;
2. SELECT `name`, `id` from `clients` WHERE `id` IN (SELECT `client_to` FROM `calls` GROUP BY `client_to`);
- Write a SQL statement to get the name and id of the clients who have no received a called yet. There must be unique results.
Answer : SELECT a.`name`, a.`id` from `clients` a LEFT JOIN `calls` b on a.`id` = b.`client_to` AND b.`client_to` IS NULL;
- Write a SQL statement to get the name and id of the clients who have not called someone yet. There must be unique results.
Answer : SELECT a.`name`, a.`id` from `clients` a LEFT JOIN `calls` b on a.`id` = b.`client_from` AND b.`client_from` IS NULL;
- Write the most simple SQL statement to get all the calls starting from the 24/01/2009 and the 03/03/2009 (start date and end date included).
Answer: SELECT `client_from`, `client_to`, `duration`, `cost`, `date` FROM `calls` WHERE `date` BETWEEN '2009-01-24' AND '2009-03-03';
- Write a SQL statement to get the total cost of all calls made by the client « JOHN » with the id 42. Only this total cost must be returned and it must be named as « total_cost »
Answer: SELECT SUM(`cost`) AS total_cost FROM `calls` WHERE `client_from` = '42';
- Write a SQL statement to get the total cost associated to each caller.
Answer: SELECT SUM(`cost`) AS total_cost FROM `calls` GROUP BY `client_from`;
- Write a SQL statement to insert a new call in the « calls » table. This call was made by the client id 42 to the client id 56 and lasted 40 minutes. The communication cost is 1 ruppee/minute. The call began at 10pm on the 21/05/2009.
Answer: INSERT INTO `calls`(`client_from`, `client_to`, `duration`, `cost`, `date`) VALUES('42', '56', '40', '40', '2009-05-21 22:00:00');
- What is not optimized in the structure of the « calls » table regarding the previous statments you wrote (Q. 3, 4, 5, 6) ? Are there something else not optimized for this table ?
Answer : Not setup foreign key in calls table `client_from` and `client_to` to `clients` table `id` column. Because the `calls` table `clients_from` and `clients_to` based on the clients table.
II. PHP, HTML and JavaScript use case
- Write a PHP code displaying the current date. The script must ouput the date in the following format: 15/08/2009 02:55
Answer:
<?php
echo date("d/m/Y H:i");
?>
- Write a simple (no formatting, no design) HTML « login » form displaying an email texbox, a password textbox and a submit button. This form must send the data to the current PHP file using the appropriate HTTP method. You are required to write the structure of the HTML document displaying the form (just the needed markup to work, no design or xHTML validation!)
Answer:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Login Form</title>
</head>
<body>
<div id="login_box">
<form name="form_login" id="form_login" action="login.php" method="post">
<fieldset>
<legend>Login</legend>
<table border="0" cellspacing="0" cellpadding="0" style="width:400px;">
<tr>
<td>Email: <em class="star">*</em></td>
<td><input type="text" name="email" id="email" value="" /></td>
</tr>
<tr>
<td>Password: <em class="star">*</em></td>
<td><input type="password" name="password" id="password" value="" /></td>
</tr>
<tr>
<td> </td>
<td><input type="submit" name="login" id="login" value="Login" />
<input type="reset" name="reset" id="reset" value="Reset" /></td>
</tr>
</table>
</fieldset>
</form>
</div>
</body>
</html>
- Add to the previous form a Javascript Validation check (email should contain an @ and a dot). Password must not be empty. In case of errors, do not use the alert() function but display an error message above the form using JavaScript.
Answer:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Login Form</title>
<script type="text/javascript" language="javascript">
function $m(id) {
return document.getElementById(id);// return html element object
}
function validate_login() {
var error = "";
var email = $m('email').value;
var password = $m('password').value;
// reset the error mesasge
$m('mosmsg').innerHTML = '';
// check email address is empty
if(email == "") {
error += "<li>Enter email address<li>";
}
else {
if(email.indexOf("@") == -1 || email.indexOf(".") == -1) {
error += "<li>Enter valid email address<li>";
}
}
if(password == "") {
error += "<li>Enter password<li>";
}
// check the error occured occured or not
if(error != "") {
$m('mosmsg').innerHTML = error;
return false;
}
return true;
}
</script>
</head>
<body>
<div id="mosmsg"> </div>
<div id="login_box">
<form name="form_login" id="form_login" action="login.php" method="post">
<fieldset>
<legend>Login</legend>
<table border="0" cellspacing="0" cellpadding="0" style="width:400px;">
<tr>
<td>Email: <em class="star">*</em></td>
<td><input type="text" name="email" id="email" value="" /></td>
</tr>
<tr>
<td>Password: <em class="star">*</em></td>
<td><input type="password" name="password" id="password" value="" /></td>
</tr>
<tr>
<td> </td>
<td><input type="submit" name="login" id="login" value="Login" onclick="return validate_login();" />
<input type="reset" name="reset" id="reset" value="Reset" /></td>
</tr>
</table>
</fieldset>
</form>
</div>
</body>
</html>
another way of calling javascript validate_login() function is
<form name="form_login" id="form_login" action="login.php" method="post" onsubmit="return validate_login();">
- Javascript can be disabled on the browser. So add to the previous form a PHP validation once the user submits the form. This validation is the same than the JavaScript one. In case of error, display an error message above the HTML form.
Answer:
<?php
// declare variable for error message
$error = "";
/**
*
* function return user input value
*
* @params name of the html elements
*
* @return string value if value exists otherwise return NULL
*/
function param_post($name, $default = false) {
// Check the name exist and it contains non empty value
if(isset($_POST[$name]) && trim($_POST[$name]) != "") return trim($_POST[$name]);
return NULL;// return null value if element not exist or it contains empty value
}
// check the user click the login button or not
if(param_post('login') == 'Login') {
// assign values to variables
$email = param_post('email');
$password = param_post('password');
// Validate inputs from user
// check email field empty
if($email == "") $error .= '<li>Enter email address<li>';
// check the email contain @ and . charector
if(strstr($email, '@') === FALSE || strstr($email, '.') === FALSE) $error .= '<li>Enter valid email address<li>';
// check password is empty
if($password == "") $error .= '<li>Enter password<li>';
// check the error mesasge is exist
if(!$error) {
// here check login information or valid or not using database and select queries
// if valid redirected to user home page using header function
}
}// endif, check login button
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Login Form</title>
<script type="text/javascript" language="javascript">
function $m(id) {
return document.getElementById(id);// return html element object
}
function validate_login() {
var error = "";
var email = $m('email').value;
var password = $m('password').value;
// reset the error mesasge
$m('mosmsg').innerHTML = '';
// check email address is empty
if(email == "") {
error += "<li>Enter email address<li>";
}
else {
// check the
if(email.indexOf("@") == -1 || email.indexOf(".") == -1) {
error += "<li>Enter valid email address<li>";
}
}
if(password == "") {
error += "<li>Enter password<li>";
}
// check the error occured occured or not
if(error != "") {
$m('mosmsg').innerHTML = "<ul>" + error + "</ul>";
return false;
}
return true;
}
</script>
</head>
<body>
<?php echo $error?'<div id="mosmsg">'. $error .'</div>':'';?>
<div id="login_box">
<form name="form_login" id="form_login" action="login.php" method="post">
<fieldset>
<legend>Login</legend>
<table border="0" cellspacing="0" cellpadding="0" style="width:400px;">
<tr>
<td>Email: <em class="star">*</em></td>
<td><input type="text" name="email" id="email" value="" /></td>
</tr>
<tr>
<td>Password: <em class="star">*</em></td>
<td><input type="password" name="password" id="password" value="" /></td>
</tr>
<tr>
<td> </td>
<td><input type="submit" name="login" id="login" value="Login" onclick="return validate_login();" />
<input type="reset" name="reset" id="reset" value="Reset" /></td>
</tr>
</table>
</fieldset>
</form>
</div>
</body>
</html>
another way of calling javascript validate_login() function is
<form name="form_login" id="form_login" action="login.php" method="post" onsubmit="return validate_login();">