What is the meaning of !$_post without a variable name

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
asker541
Forum Newbie
Posts: 2
Joined: Sat Jan 11, 2014 12:59 pm

What is the meaning of !$_post without a variable name

Post by asker541 »

I have been reading a program in PHP for creating a mailing list. The program is made up of two programs the first one is the file to be included (includeme.php) and the second is the subscription program (subscription.php)with which the subscription list is created. I met with the statement:

Code: Select all

if (!$_POST) {
//they need to see the form, so create form block
in the subscription program. The problem here that I could not understand the above !$_post statement. I would appreciate any explanation.

the includeme.php program is as follows:

Code: Select all

<?php
// function to connect to database
function doDB() {
global $mysqli;

//connect to server and select database
$mysqli = mysqli_connect(“localhost”, “joeuser”,
“somepass”, “testDB”);

if connection fails, stop script execution
if (mysqli_connect_errno()) {
printf(“Connect failed: %s\n”, mysqli_connect_error());
exit();
}
}
// function to check email address
function emailChecker($email) {
global $mysqli, $safe_email, $check_res;

//check that email is not already in list
$safe_email = mysqli_real_escape_string($mysqli, $email);
$check_sql = “SELECT id FROM SUBSCRIBERS
WHERE email = ‘“.$safe_email.”’”;
$check_res = mysqli_query($mysqli, $check_sql)
or die(mysqli_error($mysqli));
}
?>
The Subscription.php program is as follows:

Code: Select all

<?php
include ‘includeme.php’;
//determine if they need to see the form or not
if (!$_POST) {
//they need to see the form, so create form block
$display_block = <<<END_OF_BLOCK
<form method=”POST” action=”$_SERVER[PHP_SELF]”>

<p><label for=”email”>Your E-Mail Address:</label><br/>
<input type=”email” id=”email” name=”email”
size=”40” maxlength=”150” /></p>

<fieldset>
<legend>Action:</legend><br/>
<input type=”radio” id=”action_sub” name=”action”
value=”sub” checked />
<label for=”action_sub”>subscribe</label><br/>
<input type=”radio” id=”action_unsub” name=”action”
value=”unsub” />
<label for=”action_unsub”>unsubscribe</label>
</fieldset>

<button type=”submit” name=”submit” value=”submit”>Submit</button>
</form>
END_OF_BLOCK;
} else if (($_POST) && ($_POST[‘action’] == “sub”)) {
//trying to subscribe; validate email address
if ($_POST[‘email’] == “”) {
header(“Location: manage.php”);
exit;
} else {
/connect to database
doDB();

//check that email is in list
 emailChecker($_POST[‘email’]);

//get number of results and do action
if (mysqli_num_rows($check_res) < 1) {
//free result
mysqli_free_result($check_res);

//add record
$add_sql = “INSERT INTO subscribers (email)
VALUES(‘“.$safe_email.”’)”;
add_res = mysqli_query($mysqli, $add_sql)
or die(mysqli_error($mysqli));
$display_block = “<p>Thanks for signing up!</p>”;

//close connection to MySQL
mysqli_close($mysqli);
} else {
//print failure message
$display_block = “<p>You’re already subscribed!</p>”;
 }
 }
 } else if (($_POST) && ($_POST[‘action’] == “unsub”)) {
 //trying to unsubscribe; validate email address
 if ($_POST[‘email’] == “”) {
 header(“Location: manage.php”);
 exit;
 } else {
 //connect to database
 doDB();

 //check that email is in list
emailChecker($_POST[‘email’]);

 //get number of results and do action
 if (mysqli_num_rows($check_res) < 1) {
 //free result
 mysqli_free_result($check_res);

 //print failure message
 $display_block = “<p>Couldn’t find your address!</p>
 <p>No action was taken.</p>”;
 } else {
 //get value of ID from result
 while ($row = mysqli_fetch_array($check_res)) {
 $id = $row[‘id’];
 }

 //unsubscribe the address
 $del_sql = “DELETE FROM subscribers
 WHERE id = “.$id;
 $del_res = mysqli_query($mysqli, $del_sql)
 or die(mysqli_error($mysqli));
 $display_block = “<p>You’re unsubscribed!</p>”;
 }
 mysqli_close($mysqli);
 }
 }
 ?>
 <!DOCTYPE html>
 <html>
<head>
 <title>Subscribe/Unsubscribe to a Mailing List</title>
 </head>
 <body>
 <h1>Subscribe/Unsubscribe to a Mailing List</h1>
 <?php echo “$display_block”; ?>
 </body>
 </html>
User avatar
social_experiment
DevNet Master
Posts: 2793
Joined: Sun Feb 15, 2009 11:08 am
Location: .za

Re: What is the meaning of !$_post without a variable name

Post by social_experiment »

It means that if the $_POST superglobal variable isn't present the button of the form hasn't been clicked so the code needs to display a form to have the data submitted.

'!' in PHP means the Not logical operator http://au1.php.net/manual/en/language.o ... ogical.php
“Don’t worry if it doesn’t work right. If everything did, you’d be out of a job.” - Mosher’s Law of Software Engineering
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: What is the meaning of !$_post without a variable name

Post by Christopher »

That script has pretty poor logic. It would be better to see if the HTTP_METHOD is POST for whether the form has been submitted. Then check if (isset($_POST[‘email’]) && (trim($_POST[‘email’]) != '')) to see if a value has been entered.
(#10850)
asker541
Forum Newbie
Posts: 2
Joined: Sat Jan 11, 2014 12:59 pm

Re: What is the meaning of !$_post without a variable name

Post by asker541 »

So much thanks #social_experiment and #Christopher, in fact you have given me much more information about this. At first I did not realize that the program was using $_SERVER[PHP_SELF]” which is the reason behind having the $_post filled or not with something.
Post Reply