Short question what am I doing wrong?
<?php
if ($arrItem['text']['kachelband_de_external_link'] = "1"){
echo 'target="_blank"';
} else{
}
?>
I always get the output: target="_blank", even if "$arrItem['text']['kachelband_de_external_link']" = 0
Because you are doing an assignment operation instead of comparison on your if statement.
Should be
if ($arrItem['text']['kachelband_de_external_link'] == "1")
See the two equal signs ?
if ($arrItem['text']['kachelband_de_external_link'] == "1"){
echo 'target="_blank"';
}
use == to check conditions
You have to use
if ($arrItem['text']['kachelband_de_external_link'] == "1")
and not only one of "=". If you only use one, you set the var before. By using 2 "=" you compare ;)
Related
if(!isset($_GET['new_quiz']) || !isset($_GET['view_quiz']) || !isset($_GET['alter_quiz'])){
echo "No";
}
else{ echo "Yes"; }
When I go to index.php?view_quiz, it should give result as Yes, but it results as No. Why?
My Other Tries:
(!isset($_GET['new_quiz'] || $_GET['view_quiz'] || $_GET['alter_quiz']))
( ! ) Fatal error: Cannot use isset() on the result of an expression
(you can use "null !== expression" instead) in
C:\wamp\www\jainvidhya\subdomains\teacher\quiz.php on line 94
(!isset($_GET['new_quiz'],$_GET['view_quiz'],$_GET['alter_quiz']))
NO
You may find than inverting the logic makes the code easier to read, I also like to have a more positive idea of conditions as it can read easier (rather than several nots means no).
So this says if anyone of the items isset() then the answer is Yes...
if(isset($_GET['new_quiz']) || isset($_GET['view_quiz']) || isset($_GET['alter_quiz'])){
echo "Yes";
}
else{ echo "No"; }
Note that I've changed the Yes and No branches of the if around.
You are probably looking for
if(!isset($_GET['new_quiz']) && !isset($_GET['view_quiz']) && !isset($_GET['alter_quiz'])){
echo "No";
}
else {
echo "Yes";
}
which will print Yes if none of new_quiz, view_quiz and alter_quiz are present in the URL. If this is not your desired outcome, please elaborate on your problem.
#paran you need to set a value for view_quiz=yes for example
if(!isset($_GET['new_quiz']) || !isset($_GET['view_quiz']) || !isset($_GET['alter_quiz'])){
echo "No";
}
else{ echo "Yes"; }
and the url
index.php?new_quiz=yes
index.php?view_quiz=yes
index.php?alter_quiz=yes
All Will return true
isset()allows multiple params. If at least 1 param does not exist (or is NULL), isset() returns false. If all params exist, isset() return true.
So try this:
if( !isset( $_GET['new_quiz'], $_GET['view_quiz'], $_GET['alter_quiz']) ) {
First, to answer your question:
When I go to index.php?view_quiz, it should give result as Yes, but it results as No. Why?
This is becaue this
if(!isset($_GET['new_quiz']) || !isset($_GET['view_quiz']) || !isset($_GET['alter_quiz'])){
checks if either one of your parameter is not set, which will always be the case as long as you are not setting all three parameter simultaneously like this:
index.php?alter_quiz&view_quiz&new_quiz
As #nigel-ren stated, you may wan't to change that logic to
if(isset($_GET['new_quiz']) || isset($_GET['view_quiz']) || isset($_GET['alter_quiz'])){
echo 'Yes';
which checks if at least one parameter is set.
If you wan't to check if there is only one of the three parameters set, you would have to work with XOR (which is slightly more complicated)
$a = isset($_GET['new_quiz']);
$b = isset($_GET['view_quiz']);
$c = isset($_GET['alter_quiz']);
if( ($a xor $b xor $c) && !($a && $b && $c) ){
echo 'Yes';
(based on this answer: XOR of three values)
which would return true if one and only one of the three parameters is set.
But - and this is just an assumption, please correct me if I'm wrong - I think what you are trying to achieve are three different pages (one for creating a quiz, one for viewing it and one for editing it). Therefore, you will likely run into a problem with your current setup. For example: What would happen if a user calls the page with multiple parameters, like
index.php?alter_quiz&view_quiz
Would you show both pages? Would you ignore one parameter? I would recommend to work with a single parameter to avoid this problem in the first place. For example site which can take the values alter_quiz, view_quiz or new_quiz. E.g.:
index.php?site=alter_quiz
Then you can work like this:
// check if site is set before getting its value
$site = array_key_exists( 'site', $_GET ) ? $_GET['site'] : NULL;
// if it's not set e.g. index.php without parameters is called
if( is_null($site) ){
// show the start page or something
}else{
$allowed_sites = ['new_quiz', 'view_quiz', 'alter_quiz'];
// never trust user input, check if
// site is an allowed value
if( !in_array($site, $allowed_sites, true) ){
die('404 - This site is no available');
}
// here you can do whatever your site should do
// e.g. include another php script which contains
// your site
include('path/to/your/site-' . $site . '.php');
// or echo yes
echo 'Yes';
}
Can anyone explain why this:
if($xml->$ul !== ""){
echo $xml->$ul;
}
if($xml->$ul == ""){
echo "0";
}
does work, while
if($xml->$ul !== ""){
echo $xml->$ul;
}else{
echo "0";
}
does not work?
Am i missing something?
Short explanation: if the xml contains $ul it will echo its value, if it is not contained it will echo 0. Works perfectly with first code, but second code just echos the values, the else is completely ignored.
I appreciate all answers!
You are not doing the same equality check. In the first example you are first checking using !==, then in the second if you are using ==.
See this answer for an explanation of the difference between === equality and == equality. In short, === not only checks the values are equal, but also that the types of the variables being compared are the same as well.
I want to check the condition either of one among $flag1==1 or $flag2==2 or $flag3==3 along with that $flag4 == 4 is true then I want to execute a statement.This is not working in php.I tried:
if (($flag1==1 or $flag2 == 2 or $flag3 ==3) and $flag == 4)
{
$variable1 ='Change in Asset and Transformation Approach';
echo $variable1;
}
I think your problem is a typo. You mentioned you wanted $flag4 to equal 4. But you are checking the value of $flag.
if (($flag1 == 1 or $flag2 == 2 or $flag3 ==3) and $flag4 == 4) { // <-- notice $flag4
$variable1 ='Change in Asset and Transformation Approach';
echo $variable1;
}
Working Codepad.
I'm new to PHP and usually I use these lines to check if the input is empty:
if(!isset($_SESSION['z']) || strlen(trim($_SESSION['z']) == 0))
{
echo "Unknown";
}
But if the user types digit 0, a script recognises input as empty. How to change these lines to recognise 0 as a string?
Your solution should have worked. As commentator deceze pointed out, your second condition is mistyped it should be strlen(trim($_SESSION['z'])) == 0.
But alternate solution is to use strict equal check === and !==.
if(!isset($arr['z']) || trim($arr['z']) === '')
{
echo "Unknown";
}
This will work with if value is NULL or empty string, resulting 'Unknown'. But any other value would not satisfy the condition, so value 0 or 0.0 should work.
But again, your initial solution is good. Just fix the typo.
if (!isset($_SESSION['z']) || strcmp(trim($_SESSION['z']), '') == 0)
{
echo "Unknown";
}
The function strcmp returns 0, if two strings are the same.
<?php
session_start();
if(isset($_POST["z"])){$_SESSION['z']=$_POST["z"];}
if(!isset($_SESSION['z']) OR $_SESSION['z']==""){
echo "Unknown";
}else{
echo $_SESSION['z'];
}
?>
<form method="post"><input type="text" name="z"><input type="submit"></form>
This should work as you want
I just need help with a few things. Firstly how would I get the data of a selection when posted.Like
$selection=$_GET['area'];
if ($selection="time"){
echo "Not working yet please come back at a later update.";
}
and also on that note, why is it displaying it even when there is no data sent over the form. I thank any and all help.
It must be == operator you have used = operator which is the assiging operator.You have to use == or === operator for comparisons.For more check comparison operators in php
$selection = trim($_GET['area']);
if ($selection == "time"){
echo "Not working yet please come back at a later update.";
}
The operator "=" is not proper.
You should use "===" to check equality.
<?php
$selection=$_GET['area'];
if ($selection === 'time'){
echo 'Not working yet please come back at a later update.';
}
?>
Always use == or === operators to check conditions
$selection=$_GET['area'];
if ($selection=="time"){
echo "Not working yet please come back at a later update.";
}
you forgot one = your code should be
$selection = $_GET['area'];
if ( $selection == 'time' ) {
echo 'Now working yet please comde back at a later update';
}
using =you are assigning 'time' to $selection which always returnes true
use == instead.
it should be like this.
if ($selection=="time"){
$selection="time"
this assigns $selection variable a value "time".
You should use
if ($selection=="time")
instead of
if ($selection="time")