Not sure what im doing wrong here, but the out come is always null. The script should output "you did not select an answer" only if no answer was selected but otherwise it should output the answer given:
I have updated the script as mentioned but still getting the empty output even when answer is given :/
Thanks for all the help so far guys, but even the below code doesnt work, it now just outputs as blank if no anwser, but if you do fill it in, it correctly echos the answer.
if (empty( $a1 )) {
echo"<li>\n<h2>1. " . $q1[0] . "</h2>\n"
. "<p>You did not select an answer</p>\n"
. "</li>\n";
}
else {
echo"<li>\n<h2>1. " . $q1[0] . "</h2>\n"
. "<p><strong>" . $q1[$a1] . ":</strong></p>\n"
. "<p>" . $r1[$a1] . "</p>\n"
. "</li>\n";
}
Completely forgot to show this part!!
// get local copies of single answers
$a1 = trim(isset($_POST['a1'])?$_POST['a1']:99);
$a3 = trim(isset($_POST['a3'])?$_POST['a3']:99);
$a4 = trim(isset($_POST['a4'])?$_POST['a4']:99);
$a5 = trim(isset($_POST['a5'])?$_POST['a5']:99);
Don't use if($a1 == null) use if(empty($a1)) or if(isset($a1))
An empty string is not null
$a1 = '';
if ($a1 == null) // is wrong
should be
$a1 = '';
if ($a1 === '')
or
if (empty($a1))
an empty is not the same as null try
if ($a === '') this respects also the type which is better for code quality
if (empty( $a1 )) {
echo"<li>\n<h2>1. " . $q1[0] . "</h2>\n"
. "<p>You did not select an answer</p>\n"
. "</li>\n";
}
else {
echo"<li>\n<h2>1. " . $q1[0] . "</h2>\n"
. "<p><strong>" . $q1[$a1] . ":</strong></p>\n"
. "<p>" . $r1[$a1] . "</p>\n"
. "</li>\n";
}
Use empty instead of null checking
'null' is not same as false or ''.'null' is an object.
In PHP, empty string ($a) & empty array ($b) will return true if you test following express:
$a = ''; $b = array();
$a == null -> TRUE $b == null -> TRUE
also,
$a == 0 -> TRUE
So you should use '===' to test, or there's always unexpected result in your code.
Related
I have an php in a server and i found an issue (solved). The issue was in a comparison among to long integer saved in a string and I can not use Equal, I have to use Identical. But I do not know why I have to use it, if it is a comparison among to strings.
I make a test, and I get two different results, in local host and server.
Server PHP version: 5.3.10-1ubuntu3.10
Local PHP version: 5.6.30-7+deb.sury.org~xenial+1
Here is mi code:
<?php
$a = "1285615000003961035";
$b = "1285615000003961023";
if($a == $b)
{
echo "$a == $b<br>";
}
else
{
echo "$a != $b<br>";
}
if((string)$a == (string)$b)
{
echo "(string)" . (string)$a . " == (string)" . (string)$b . "<br>";
}
else
{
echo "(string)" . (string)$a . " != (string)" . (string)$b . "<br>";
}
if($a === $b)
{
echo "$a === $b<br>";
}
else
{
echo "$a !== $b<br>";
}
if((string)$a === (string)$b)
{
echo "(string)" . (string)$a . " === (string)" . (string)$b . "<br>";
}
else
{
echo "(string)" . (string)$a . " !== (string)" . (string)$b . "<br>";
}
?>
In Server I get:
1285615000003961035 == 1285615000003961023
(string)1285615000003961035 == (string)1285615000003961023
1285615000003961035 !== 1285615000003961023
(string)1285615000003961035 !== (string)1285615000003961023
and in local:
1285615000003961035 != 1285615000003961023
(string)1285615000003961035 != (string)1285615000003961023
1285615000003961035 !== 1285615000003961023
(string)1285615000003961035 !== (string)1285615000003961023
Since PHP 5.4, according to the release notes, integral strings that overflow into floating point numbers will no longer be considered equal, because of the way float numbers are represented internally.
For more information see the links below:
http://php.net/manual/en/language.operators.comparison.php (Warning section after the examples)
http://php.net/manual/en/types.comparisons.php#108264
You should use strcmp or === for comparing strings (as you have used), because when you use == PHP will do type conversions.
http://php.net/manual/en/language.operators.comparison.php
Edit: As Lucas Mendes stated above, this is only partially true after PHP 5.4 - but still it is safer to use operators that do not do type conversions by default to compare strings.
I want to display a specific value from key value list..
here is my code:
if (isset($_POST) && count($_POST)>0 )
{
foreach($_POST as $paramName => $paramValue) {
echo "<br/>" . $paramName . " = " . $paramValue;
}
}
ouput
ORDERID = ORDS3700373
TXNAMOUNT = 200.00
CURRENCY = INR
TXNID = 32221284
BANKTXNID = 475815
STATUS = TXN_SUCCESS
RESPCODE = 01
RESPMSG = Txn Successful.
TXNDATE = 2017-01-10 18:13:25.0
GATEWAYNAME = WALLET
BANKNAME =
PAYMENTMODE = PPI
CHECKSUMHASH =
here I want to display only ORDERID and TXNID.. How do I get that value?
You can easily access post values by it's field name instead of looping through all post elements. Simply access that elements directly as below:
if(isset($_POST['ORDERID'])) {
echo 'ORDERID = '.$_POST['ORDERID'];
}
if(isset($_POST['TXNID'])) {
echo 'TXNID= '.$_POST['TXNID'];
}
Moving comments to an answer.
You do not need to loop post it is just a global array. You can access the values at any of the keys like any associative array because that is what it is. Likewise these value can be used like any other
if(isset($_POST['ORDERID'])){
$orderid = $_POST['ORDERID'];
}
if(isset($_POST['TXNID'])){
$txnid = $_POST['TXNID'];
}
// Should use htmlspecialchars() or htmlentities() here
// but didn't want to confuse OP. It is for security.
echo "ORDERID is: " . $orderid . " and TXNID is: " . $txnid;
A note for security never trust user input and sanitize all $_POST variables before echoing or persisting. There are far better article out on the internet than I can summarize here.
You can use if condition in the loop like this
if (isset($_POST) && count($_POST)>0 )
{
foreach($_POST as $paramName => $paramValue) {
if($paramName == 'ORDERID' || $paramName == 'TXNID')
echo "<br/>" . $paramName . " = " . $paramValue;
}
}
add an if like
if($paramName == "ORDERID" || $paramName == "TXNID") {
after foreach, remeber to close it after echo statement line
Don't overcomplicate a trivial task with a loop.
Just drop the loop and echo the two values directly:
// Assuming the two values are expected to come in pair:
if(isset($_POST['ORDERID']) && isset($_POST['TXNID'])) {
echo "<br/>ORDERID = " . $_POST['ORDERID'];
echo "<br/>TXNID = " . $_POST['TXNID'];
}
If you insist on having a loop, then you can go through the property names which you need
foreach(array('ORDERID', 'TXNID') as $paramName) {
if(isset($_POST[$paramName])) {
echo "<br/>" . $paramName . " = " . $_POST[$paramName];
}
}
function check($text){
if(strpos('a', $text) == FALSE && strpos('b', $text) == FALSE){
echo 'error';
} else {
echo 'ok';
}
}
echo check('text') . "\n";
echo check('asd') . "\n";
echo check('bnm') . "\n";
echo check('test') . "\n";
echo check('abc') . "\n";
live: http://codepad.org/W025YYuH
why this not working? This return:
1 error 2 error 3 error 4 error 5 error
but should be:
1 error 2 ok 3 ok 4 error 5 ok
You should use === FALSE instead of == FALSE, as explained in the documentation
Additionally, your arguments are in the wrong order. Again, consult the documentation (or, as some people say, RTM)
Invert the position of the argument, first argument is the string, second argument is what do you search into the string.
strpos ( 'The string to search in' ,'the argument of search' )
Then == would not work as expected
because the position of 'a' was the 0th (first) character.
Try this:
function check($text){
if(strpos($text, 'a') === FALSE && strpos($text, 'b') === FALSE){
echo 'error';
} else {
echo 'ok';
}
}
echo check('text') . "\n";
echo check('asd') . "\n";
echo check('bnm') . "\n";
echo check('test') . "\n";
echo check('abc') . "\n";
You have the arfuments the wrong way around, change to:
if(strpos($text, 'a') === FALSE && strpos($text, 'b') === FALSE){
Also note that you need to check for a boolean false with the identical operator (===)
find() is returning NULL even though there is a row that matches the criteria.
Find snippet
$tempApp = Applicant::model()->find(array('condition'=>'phn=' . $app->phn . ' AND id<>' . $app->id));
if($tempApp != NULL) {
$archId = $this->archive($tempApp);
if($archId != NULL) {
$tempApp->phn = NULL;
$tempApp->save();
$app->note = 'Former name: ' . $tempApp->first_name . ' ' . $tempApp->middle_name . ' ' . $tempApp->last_name;
} else {
unset($archId);
}
}
NOTE: This code works the second time the applicant is updated. I'm confused why this is happening. Can someone give me advice as to why this is happening.
NOTE: I tried the different ways find() can be used (ie. find('phn=:phn AND id<>:id', array(':phn'=>$app->phn, ':id'=>$app->id));
Thanks
use count() instead of NULL
if(count($tempApp)) {
$archId = $this->archive($tempApp);
if(count($archId)) {
$tempApp->phn = NULL;
$tempApp->save();
$app->note = 'Former name: ' . $tempApp->first_name . ' ' . $tempApp->middle_name . ' ' . $tempApp->last_name;
} else {
unset($archId);
}
}
Try echoing out your SQL statement and pasting it in phpmyadmin sql tab and see if you get a result.
Also, I would try using !empty() instead of NULL for my if statement
I'm sometimes confused to using which one of them,
say I have a function called getmember($id)
function getmember($id)
{
// now this is the confusing part
// how do i test if a $id was set or not set?
//solution 1
if(empty($id))
{
return false;
}
// solution 2
if(isset($id))
{
return false;
}
}
That's sometimes not clear to me, sometimes if a parameter in a function is set like function($var="")
Then I do
if($var ==="")
{
return false;
}
What should I use the next time isset ? empty ? or ===''?
Here you go, a complete breakdown of what works and when:
<?
echo "<pre>";
$nullVariable = null;
echo 'is_null($nullVariable) = ' . (is_null($nullVariable) ? 'TRUE' : 'FALSE') . "\n";
echo 'empty($nullVariable) = ' . (empty($nullVariable) ? 'TRUE' : 'FALSE') . "\n";
echo 'isset($nullVariable) = ' . (isset($nullVariable) ? 'TRUE' : 'FALSE') . "\n";
echo '(bool)$nullVariable = ' . ($nullVariable ? 'TRUE' : 'FALSE') . "\n\n";
$emptyString = '';
echo 'is_null($emptyString) = ' . (is_null($emptyString) ? 'TRUE' : 'FALSE') . "\n";
echo 'empty($emptyString) = ' . (empty($emptyString) ? 'TRUE' : 'FALSE') . "\n";
echo 'isset($emptyString) = ' . (isset($emptyString) ? 'TRUE' : 'FALSE') . "\n";
echo '(bool)$emptyString = ' . ($emptyString ? 'TRUE' : 'FALSE') . "\n\n";
//note that the only one that won't throw an error is isset()
echo 'is_null($nonexistantVariable) = ' . (#is_null($nonexistantVariable) ? 'TRUE' : 'FALSE') . "\n";
echo 'empty($nonexistantVariable) = ' . (#empty($nonexistantVariable) ? 'TRUE' : 'FALSE') . "\n";
echo 'isset($nonexistantVariable) = ' . (isset($nonexistantVariable) ? 'TRUE' : 'FALSE') . "\n";
echo '(bool)$nonexistantVariable = ' . (#$nonexistantVariable ? 'TRUE' : 'FALSE') . "\n\n";
?>
THE OUTPUT:
is_null($nullVariable) = TRUE
empty($nullVariable) = TRUE
isset($nullVariable) = FALSE
(bool)$nullVariable = FALSE
is_null($emptyString) = FALSE
empty($emptyString) = TRUE
isset($emptyString) = TRUE
(bool)$emptyString = FALSE
is_null($nonexistantVariable) = TRUE
empty($nonexistantVariable) = TRUE
isset($nonexistantVariable) = FALSE
(bool)$nonexistantVariable = FALSE
When I show (bool)$variable above, that is how you could use it in a conditional. For example, to check if a variable is null or empty, you could do:
if (!$variable)
echo "variable is either null or empty!";
But it's best to use a function since it's a little more readable. But it's your choice.
Also, check the PHP type comparison table. It's basically what I just did above, except much more.
If you simply want to know if a variable is defined, use isset()
If you want to see if it's been initialized, use is_null()
If you want to compare it's value to something else, use ==
Not the same:
isset: Determine if a variable is set and is not NULL
http://ch.php.net/manual/en/function.isset.php
empty: Determine whether a variable is empty
http://ch.php.net/manual/en/function.empty.php
$a===$b: TRUE if $a is equal to $b, and they are of the same type.
http://ch.php.net/manual/en/language.operators.comparison.php