Php condition doesn't work for html [closed] - php

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I currently want to display a link only on a certain page. The architecture on the website itself is over the index.php?menu=<pagename>. So I thought I could simply use:
<?php
echo $_GET["menu"];
if($_GET["menu"] = "mMenu-Point") :
?>
Test
<?php
endif;
?>
to execute the htm-code. However it seems like the code will be executed regardless what the value of the "menu" is. I tested this through the echo which strangely gets me the correct values.
I hope you can explain me why this doesn't work.
Thank you very much for your time and efforts!

try
if($_GET["menu"] == "mMenu-Point") :
At the moment you are setting (=) $_GET["menu"] not comparing it (==)

The assignment operator (=) is used to assign a value to a variable, element of an array, or property of an object
The equality operator (==) is used to compare two values or expressions. It is used to compare numbers, strings, Boolean values, variables, objects, arrays, or functions.
You're using the assignment operator = here. You need
if($_GET["menu"] == "mMenu-Point") :
Hope this helps!

Your using the assignment operator (which is one equals) You need to use the comparison operator (==) or even (=== For an exact match)
http://php.net/manual/en/language.operators.comparison.php
So you need to be using:
if ($_GET["menu"]) == "mMenu-Point") :
For comparing to strings, I would recommend using the exact match operator ===

Double "=" :
if($_GET["menu"] == "mMenu-Point")

if($_GET["menu"] == "mMenu-Point") :
tow "=" pls

Related

== operator for string [duplicate]

This question already has answers here:
Understanding PHP type coercion
(4 answers)
Closed 7 years ago.
Why does this code echo Yes. even though variables are not equal!
$a = '0e462097431906509019562988736854';
$b = '0e830400451993494058024219903391';
if( $a == $b ) echo 'Yes.';
else echo 'No!';
Both will treated as numbers, and PHP had limitations in number storage before.
So check that.
Try to use '==='. it will check the type also, so those will not convert to numbers.
Refer this question and its answers.
You want strcmp, not the equality operator.
try it, with using strcmp function:
if(int strcmp ($a,$b)===0) echo 'Yes.';
else echo 'No!';
Try using '===' instead of '=='.
'==' has a "weaker" comparison because it does not check for type.
'===' on the other hand, checks for the type as well, and it is generally good practice to be more explicit when you compare two things.

Ternary Operators and exit statements [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 8 years ago.
Improve this question
Hey everyone I have this one line of code, and I was thinking about something, is this considered a legal assignment of a variable or will it cause errors. Furthermore is it okay to use exit() statements like this, or am I just terrible at coding somedays? Also if there is a duplicate question like this, please point me in the right direction that would be fanastic!
list($foo, $bar) ? generateValues($data) : exit("Unable to obtain useful information);
The list() you are using will assign $foo and $bar values if you use it like so:
list($foo,$bar) = array('fooValue', 'barValue');
so to properly use it in a tertiary statement would be like so:
list($foo, $bar) = (conditional) ? generateValues($data) : exit('...');
the exit will fire if the conditional is false, otherwise the array generated by generateValues() will be returned by the assignment, and list() will assign the values respectively.
Documentation.

What is the difference between if(isset($a)) and if($a) in php? [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
Closed 8 years ago.
Improve this question
What is the difference between if(isset($a)) and if($a) or if_exist($a) and if($a) in php?
With $a = false;:
if ($a) {} will return false, whereas if (isset($a)) {} will return true.
I do not know that if_exist you speak of. :)
Edit: Please check #Utkanos's answer for an excellent and more expansive explanation. :)
if (isset($var))
checks that a variable has been set and that it has a non-null value.
if ($var)
assumes the variable has been set and checks instead for it having a truthy value.
PHP has no function if_exist. Perhaps you're thinking of array_key_exists, which returns true if an array contains a certain key.
isset($var) checks wether the variable $var is not of type null and returns a boolean true or false.
A variable is considered to be null if:
it has been assigned the constant NULL.
it has not been set to any value yet.
it has been unset().
if($var) does an implicit type conversion to convert the $var from its original type to boolean which means it checks for truthy or falsy values.
To check wether a variable is truthy you can explicit convert it to a boolean:
$var = "foo";
var_dump((bool)$var); // outputs 'bool(true)'
$var = "0";
var_dump((bool)$var); // outputs 'bool(false)'
For a full list of how PHP handles the diffrent variable types see:
http://www.php.net/manual/en/types.comparisons.php

Php short if fails [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 9 years ago.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Improve this question
I have a little problem with "short if" in Php.
isset($this->sets[$this->value])?$this->sets[$this->value]:$this->value
this drops notices. It looks the $this->sets[$this->value] runs even when it doesnt exists. If I do:
if (!isset($this->sets[$this->value]))
{
$this->sets[$this->value] = '';
}
it does solve the problem, but then I dont understand something....
EDIT: I refactored:
if (isset($this->sets[$this->value]))
{
$value = $this->sets[$this->value];
}
else
{
$value = $this->value;
}
return $value;
and it works, dunno why....
return 'something'.isset($this->sets[$this->value])?$this->sets[$this->value]:$this->val‌​ue;
'something'.isset($this->sets[$this->value]) always evaluates to true. You'll need to group the ternary operator expression:
return 'something' . (isset($this->sets[$this->value]) ? $this->sets[$this->value] : $this->val‌​ue);
And that's why you always post a complete example in your question, not just a subset!
It is not true that both operands are evaluated. Try this to see:
true?print('1'):print('2');
Only the '1' prints.
The issue is that your first line of code does not do anything in and of itself. You don't assign the result of the expression to anything, you don't use it anywhere, I would not be suprised if zend just discards it.
In your second example, you explicitly create an array element if it does not already exist. If you wanted to do the same thing with the ternary operator, you could do
isset($this->sets[$this->value])?null:($this->sets[$this->value]='');
I do not know why you would want to, but it would achieve the same thing as you did in your second example.
Your refactored example can be accomplished using the ternary operator as:
return isset($this->sets[$this->value])?$this->sets[$this->value]:$this->value;
This is a typical usage of the ternary operator

if statement and $_GET [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I am passing a variable using GET, and I'd like a fallback if there's nothing there. I've been using
$page = $_get;
if $page = ""
{
include 'home.php';
}
else
{
include $page;
}
But it just breaks my page. Any ideas?
First, $_GET is in capitals.
Second, you need to say which value_name you want. $_GET['value_name']
Third, you need to check if the value_name has been set. if(isset($_GET['value_name']))
Fourth, and most important: NEVER DO THIS ! Never include files based on what is given in the url or otherwise user input. You will be hacked ! Rather you match the given value to an array of allowed file names or use a switch.
Probably just missing the parens on the if statement. Also, you'll need to specify the GET param.
$page = $_GET['param'];
if ($page == "") {
include 'home.php';
} else {
include $page;
}
First of all, $_GET is an associative array. So you should check against something, or not empty.
On the other hand, here $page = "" you're assigning; if you want to compare, you need to use == or ===. Also, you could use functions like isset, or empty, that will return you a bool if there is an element or not in the variable.
So, what you need is something like this
include !empty($_GET['param'])?$_GET['param']:'home.php'
? is a ternary operator, is a resumed if-else
As last aclaration, you should never include directly variables you receive from $_GET, and $_POST. This will put risk on your system. example on Stack Overflow
Also, use isset() to check if you have an input...
But yes what you want to do is a little bit dangerous in term of security... !

Categories