Jump to content

Is True True?


doubledee

Recommended Posts

Not exactly, all values passed via the querystring are strings.

 

Having said that however, there are a number of values that will return true in php. See this page: http://php.net/manual/en/language.types.boolean.php

 

Pretty lose way to define "TRUE"?!

 

Should I type-cast my query string value, or is what I am doing above save enough?

 

 

Debbie

Link to comment
https://forums.phpfreaks.com/topic/254566-is-true-true/#findComment-1305373
Share on other sites

Should I type-cast my query string value, or is what I am doing above save enough?

 

Do you plan on ever using &addcomment=false?  If not, then I would just stick with the isset() check and ignore what the value is.

 

If you do think you'll be using a false value, then I would do the string comparison:

$addcomment = (isset($_GET['addcomment']) && $_GET['addcomment']=='true');

 

Note that typecasting to a bool would not yield a true result because the string is 'true', but because it's not '' (empty) or '0' (zero).  For instance, the following:

$value = 'false';
$bool = (bool)$value;

 

$bool would be true, not false.  So if you do pass a 'true'/'false' you need to test them as a string, not just typecast.  If you instead use '1'/'0' then you can typecast to either bool or int and it will work ok.

 

Link to comment
https://forums.phpfreaks.com/topic/254566-is-true-true/#findComment-1305380
Share on other sites

Here is the code I went with...

// ******************
// Redirect User.		*
// ******************

// Add a Comment Redirect.
if (isset($_GET['addComment']) && ($_GET['addComment']==TRUE)){
	header("Location: " . BASE_URL . "articles/add_comment.php");

	// End script.
	exit();
}

// Normal Redirect.
if (isset($_SESSION['returnToPage'])){
	header("Location: " . BASE_URL . $_SESSION['returnToPage']);
}else{
	// Take user to Home Page.
	header("Location: " . BASE_URL . "index.php");
}

 

 

Debbie

 

Link to comment
https://forums.phpfreaks.com/topic/254566-is-true-true/#findComment-1305387
Share on other sites

Here is the code I went with...

// ******************
// Redirect User.		*
// ******************

// Add a Comment Redirect.
if (isset($_GET['addComment']) && ($_GET['addComment']==TRUE)){
	header("Location: " . BASE_URL . "articles/add_comment.php");

	// End script.
	exit();
}

// Normal Redirect.
if (isset($_SESSION['returnToPage'])){
	header("Location: " . BASE_URL . $_SESSION['returnToPage']);
}else{
	// Take user to Home Page.
	header("Location: " . BASE_URL . "index.php");
}

 

 

Debbie

 

 

With this code, if "$_GET['addComment']" is equal to ANYTHING, the check will pass. If you only want it to pass when it is set to "true", then you need to do:

if (isset($_GET['addComment']) && ($_GET['addComment']=='true')){

Link to comment
https://forums.phpfreaks.com/topic/254566-is-true-true/#findComment-1305391
Share on other sites

With this code, if "$_GET['addComment']" is equal to ANYTHING, the check will pass. If you only want it to pass when it is set to "true", then you need to do:

if (isset($_GET['addComment']) && ($_GET['addComment']=='true')){

 

Oops!!

 

Would it be better if I did things like this...

echo '<p>To leave a comment, you need to...</p>
	<ul class="button2">
		<li>
			<a href="' . BASE_URL . 'members/log_in.php?addComment=1">Log In</a>
		</li>

 

 

Debbie

Link to comment
https://forums.phpfreaks.com/topic/254566-is-true-true/#findComment-1305393
Share on other sites

With this code, if "$_GET['addComment']" is equal to ANYTHING, the check will pass. If you only want it to pass when it is set to "true", then you need to do:

if (isset($_GET['addComment']) && ($_GET['addComment']=='true')){

 

Oops!!

 

Would it be better if I did things like this...

echo '<p>To leave a comment, you need to...</p>
	<ul class="button2">
		<li>
			<a href="' . BASE_URL . 'members/log_in.php?addComment=1">Log In</a>
		</li>

 

 

Debbie

 

No. 1 is not a boolean. It will evaluate to true, but any value will do the same. If you want it to only ever work if it says ?addComment=true, then you need to check that the string is "true".

Link to comment
https://forums.phpfreaks.com/topic/254566-is-true-true/#findComment-1305403
Share on other sites

No. 1 is not a boolean. It will evaluate to true, but any value will do the same. If you want it to only ever work if it says ?addComment=true, then you need to check that the string is "true".

 

Yeah, I think I'll just do...

 

$_GET['addComment']=='true'

 

Thanks,

 

 

Debbie

 

 

Link to comment
https://forums.phpfreaks.com/topic/254566-is-true-true/#findComment-1305417
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.