same code, different servers, different output - php

The code in question :
<?php /*tests added by jason*/
echo "<br />";
echo "count = " . $this->countModules('showcase');
echo "<br />";
echo "hidebyview = " . $hideByView;
echo "<br />";
if($hidebyview == true) {
echo "T";
}
else {
echo "F";
}
echo "<br />";
if ($this->countModules('showcase') && $hideByView == false) {
echo "pass";
}
else {
echo "fail";
}
echo "<br />";
?>
Site 1 output Apache/2.2.22 (Ubuntu) PHP Version 5.3.10-1ubuntu3.7 (where everything works fine):
count = 1
hidebyview =
F
pass
Site 2 output Apache/2.2.13 (Win32) PHP/5.3.26 (where the thing is broken) :
count = 1
hidebyview = 1
F
fail
I guess it boils down to how can the part that evaluates to "fail" evaluate to different answers?

$hideByView == false is not (always) equal to !($hidebyview == true) because of casting and other possible automatic conversions. So your debug info is not really showing you what your expression $hideByView == false evaluates to.

Related

PHP - $_POST['variable'] echos correctly but returns false in if statement

I have a htm file that passes a variable called $team to a php file. The php file echo's 'Fred' just fine on line 3 if that is what the user inputs but an if statement which asks if $_POST["team"] == "Fred" always returns negative. Can anyone help?
<?php
echo $_POST["team"] , "<br />";
if ($_POST["team"] == "Fred"){
echo "You go for " , $_POST["team"];
}
else {
echo "You do NOT go for Fred";
}
?>
Output:
Fred
You do NOT go for Fred
I think $_POST["team"] has spaces. Try this:
<?php
echo $_POST["team"] , "<br />";
if (trim($_POST["team"]) == "Fred"){
echo "You go for " , $_POST["team"];
}
else {
echo "You do NOT go for Fred";
}
?>
Note: this code is not related to the (,) Because you using echo, not merge string
If your $_POST['team'] contains spaces, the comparison will always return false.
Try to use trim() (Documentation here). Trim remove all spaces in your string, so it will reduce chances to fall in errors like these.
<?php
$team = trim($_POST['team']);
echo $team , "<br />";
if ($team == "Fred"){
echo "You go for " , $_POST["team"];
}
else {
echo "You do NOT go for Fred";
}
?>
Let me know if it works =D
you can echo like this because merge string in php use .
if ($_POST["team"] == "Fred"){
echo "You go for " . $_POST["team"];
}

Foreach loop echo

The last foreach loop doesn't seem to echo the list I want, it doesn't print anything. How can I fix this? I know it's getting quite complicated but any help would be appreciated.
$allTroopsList = array();
$allMissionsList = array();
while ($mytroops = $alltroops->fetch_assoc())
{
$allTroopsList []= $mytroops;
}
while ($mymissions = $allmissions->fetch_assoc())
{
$allMissionsList []= $mymissions;
}
while($userintroop = $allUsersintroops->fetch_assoc())
{
if($userintroop['userid'] == $_SESSION['userid'])
{
echo "<ul class='troop'>";
echo "<li>" . $userintroop['troopid']. " </li>";
foreach($allTroopsList as $mytroops)
{
if($userintroop['troopid'] == $mytroops['troopid'])
{
echo "<li> Troop description: " . $mytroops['description']. " </li>";
foreach($allMissionsList as $mymissions)
{
if($mytroops['missionid'] == $mymissions['missionid'])
{
echo "<li> Missionname: " . $mymissions['missionname']. " </li>";
echo "<br/>";
echo "</ul>";
}
}
}
}
}
}
foreach($allUsers as $myotherusers)
{
if($userintroop['userid'] == $myotherusers['userid'])
{
echo "<li> other users: " . $myotherusers['username']. " </li>";
echo "<br/>";
}
}
This line:
while($userintroop = $allUsersintroops->fetch_assoc())
is fetching results from a database, right? That'd mean at some point you run out of rows to fetch, and $userintroop will become a boolean FALSE.
Then later on you try to do
if($userintroop['userid'] == $myotherusers['userid'])
which is wrong on two levels - $userintroop will NOT be an array at this point, so it could never have a ['userid'] parameter. And since it's the result of a failed fetch operation, never could have any value OTHER than a FALSE.
So that last loop does execute, but will never produce anything since the conditions for producing output are literally impossible to meet.

Having Trouble with an odd/even script

I'm trying to make a script that outputs user names based on weather they are assigned an odd or even value. I think I've managed to get the odd ones working but the even ones wont output. Here is what it looks like. The 'commentid' is the value which determines if they are to be assigned to odd or even.
<?php
$db = mysql_connect('localhost','username','pass') or die("Database error");
mysql_select_db('dbname', $db);
$query2 = "SELECT * FROM comments";
$result2 = mysql_query($query2);
while ($row2 = mysql_fetch_assoc($result2))
if ( $row2['commentid'] % 2 )
{
echo $row2['name'];
echo "<br />";
}
elseif (!$row2['commentid'] % 2)
{
echo $row2['name'];
}
else
{
echo "";
}
?>
I think it has something to do with either the $row2['name'] or (!$row2['commentid'] % 2) or maybe I need to assign rows to strings or something but I can't figure out what I'm doing wrong.
EDIT I really should have made it more clear exactly what I am trying to do with this script because while your answers make sense they dont solve my problem. On the same page as this script I have another script running. This script calls a value from a different table in the same database. It's running on a timed cron event that resets daily so the value is different every day. It looks like this and is working as it should.
<?
$db = mysql_connect('localhost','username','pass') or die("Database error");
mysql_select_db('dbnamesameasother', $db);
$query = "SELECT pool FROM winners";
$result = mysql_query($query) or die(mysql_error());
while ($row = mysql_fetch_array($result))
if ( $row['pool'] % 2 )
{
echo "<h4>Result 1</h4>";
echo "<br />";
}
else
{
echo "<h4>Result 2</h4>";
echo "<br />";
}
?>
What I am trying to do is only call up the names associated with either the odd or even value based on the number the script above dictates. So if this script chooses Result 1, I only want to display the user names who would fall under Result 1.
It is sufficient to do a:
if ($row2['commentid'] % 2)
{
// odd
}
else
{
// even
}
If you want to know whats wrong with your logic, you should learn something about operator precedence in PHP. According to the chart, ! has higher precendence than %. The expression !$row2['commentid'] % 2 is thus evaluated as follows:
when $row2['commentid'] is 0
!0 = true
true % 2 = 1
when $row2['commentid'] is 1
!1 = false
false % 2 = 0
when $row2['commentid'] is 2
!2 = false
false % 2 = 0
Hope this helps
if ( $row2['commentid'] % 2 ==0 )
{
echo $row2['name'];
echo "<br />";
}
else
{
echo $row2['name'];
}
if ( $row2['commentid'] % 2 )
{
echo $row2['name'];
echo "<br />";
}
elseif (!$row2['commentid'] % 2)
{
echo $row2['name'];
}
else
{
echo "";
}
replace this code with
if ( $row2['commentid'] % 2 )
{
echo $row2['name'];
echo "<br />";
}
else
{
echo $row2['name'];
}
since there is only two posibility either it commentid divisible by 2 or not divisible by 2
There is a problem. If you delete a comment, you will get 2 adiacent rows with/without <br>. You should use a an iterator variable, like this:
$i=1;
while ($row2 = mysql_fetch_assoc($result2)){
echo $row2['name'];
if($i % 2 == 0){
echo "<br />";
}
$i++;
}

Comparing 2 exact same strings returns false

I have a variable that is posted through a html form:
$_POST['ref']
And a variable that is pulled from a table in a database:
$row['ref']
i have a basic comparison script to check if they are both the same:
$ref = $_POST['ref'];
$result = mysql_query("SELECT * FROM logbook.job");
if (!$result) {
die("Query to show fields from table failed");
}
$row = mysql_fetch_array($result);
$refdb = $row['ref'];
$refform = $_POST['ref'];
echo $_POST['ref'] ."<br>". $row['ref'] . "<br><br>";
if ($refdb == $refform) {
echo "Yes they are<br><br>";
}
else {
echo "No they are not<br><br>";
}
if (is_string($_POST['ref']))
{
echo "Yes";
} else {
echo "No";
}
echo "<br>";
if (is_string($row['ref']))
{
echo "Yes";
} else {
echo "No";
}
Which outputs:
G2mtxW
G2mtxW
No they are not
Yes
Yes
I echo them both out. Than i ask if they are the same. Then i check whether each is a string.
How come they are not the same? How can i get them to match
Any help would be appreciated
Try using the binary-safe comparison for String:
result = strcmp($str1, $str2);
If the result is 0, then both are the same. Otherwise, they aren't.
One of your strings (probably the one from the DB) might be null-terminated. I've tested the following
$foo = "abc\0";
$bar = "abc";
echo "$foo\n$bar\n";
if($foo == $bar)
echo "Equal.";
else
echo "Not equal."
Output is
abc
abc
Not equal.
Try var_dump-ing both values, check their lengths and inspect them using view-source. They are different in someway.
echo $status_message;
echo "Accepted";
if(strcmp($status_message,"Accepted")==0)
{
echo "equal";
}
else
{
echo "not equal";
}
?>
$row['status'] is a field from table

Php function in echo? Is it possible?

Hi I have a special problem ... I have three values from database.
Value1 is 1 or 0
Value2 is again 1 or 0
Value3 is remaining time like (24 hours left, 23, ...)
There values are saved in variables:
$value1
$value2
$value3
These values change from DB on every load from webpage. I have these values also inside echo""; The php function is already like:
echo"text text text .................
"Value 1 is:" . $value1 . " and value 2 is:" . $value2 . ""
..................;
I need a function that says different things like
if (value1=0)
echo "Only text"
else
echo "Value 1 is:" . $value1 . " and value 2 is:" . $value2 . "";
this to be in another echo function from first example so in my way it looks like this:
*Some function*
echo"if (value1=0)
echo "Only text"
else
echo "Value 1 is:" . $value1 . " and value 2 is:" . $value2 . """; // two echo closing
But it does not work. Any help will be appreciated.How to solve this? thank you.
Echo is saying to output whatever you type. Saying to output "echo" actually means to display the word "echo". There's no reason to output an output... you already output it!
I think what you want is something more like:
if($value1 == 0){
echo "Only text";
} else {
echo "Value 1 is:" . $value1 . " and value 2 is:" . $value2;
}
Your second piece of code looks fine. Having multiple echo statements is not a problem, and using if...else to choose one is perfectly reasonable.
If you have to use just one for some reason, you can use the (...) ? ... : ... ternary operator.
echo (value1 == 0) ? 'Only text' : "Value 1 is:" . $value1 . " and value 2 is:" . $value2 . """;
function chVal($value1, $value2) {
if ($value1 == 0) {
echo "Only text";
} else {
echo "Value 1 is:" . $value1 . " and Value 2 is:" . $value2;
}
}
Is that what you are looking for?
EDIT: I think I get what you mean.
function chVal ($val) {
if ($val == 0) {
return true;
} else {
return false;
}
}
if ($value1) {
echo "Value 1 is:" . $value1 . "\n";
} else {
echo "Only Text\n";
}
if ($value2) {
echo "Value 2 is:" . $value2 . "\n";
} else {
echo "Only Text\n";
}
Please use value1==0. Basically = means you assign the value 0 into the variable value1.
Why not use something like this:
echo "Value 1 : ".func1($value1)." - Value 2 : ".func2($value2)." - Value 3 : ".func3($value3);
then you may have 3 functions or 1 depending on how complex is your logic.
function func1($value){
if ($value == 0) return " zero ";
else return " else ";
}
Probably in this way,
echo 'if (value1=0)
echo "Only text"
else
echo "Value 1 is:"' . $value1 . " and value 2 is:" . $value2 ;
How about something like:
<?php
$value1=0;
$value2=1;
echo ($value1==0) ? "value 1 is :".$value1." and value 2 is ".$value2 : "only text";
?>
Is this what you're looking for?
I think you're looking to display the PHP code itself: http://se.php.net/manual/en/function.highlight-string.php
<?php
highlight_string('
function chkValue($value1,$value2) {
if($value1 == 0) {
echo "Only Text<br />";
} else {
echo "Value 1 is: ".$value1." and Value 2 is: ".$value2."<br />";
}
}
');
?>
It's very hard to tell what you're trying to achieve, but based on some of your other comments I'm beginning to suspect you want to echo PHP code:
$value = ($value == 0 ? 'Only Text' : "Value 1 is:$value1 and value2 is: $value2");
echo "echo \"$value\";";
update:
Yes thats it! Working but I need different colors for the two texts :-) How to add there?
I'm assuming you're outputting the text to a browser, so something like this will work:
$value = '<span style="color:#'
. ($value == 0 ? 'f00">Only Text' : "0f0\">Value 1 is: $value1 and value2 is: $value2")
. '</span>';
echo "echo \"$value\";";
echo ($value1 == 0)?'Yes':'No';

Categories