check wether cookies are available or not
$d=0;
//**data is stored in cookies as arrays**
if(is_array($_COOKIE['data']) {
//**data increment by 1 if found**
$d=$d+1;
}
//**if data not found echo data not found**
if($d==0) {
echo "data is not present";
}
else{
echo "data presrnt";
}
I am getting notice undefined variable data
use isset to check vars set or not:
$d=0;
//**data is stored in cookies as arrays**
if(isset($_COOKIE['data']))
if(is_array($_COOKIE['data']){
//**data increment by 1 if found**
$d=$d+1;
}
//**if data not found echo data not found**
if($d==0){
echo "data is not present";
}
else{
echo "data presrnt";
}
you can check the existance of a variable with isset() function like this :
if(isset($_COOKIE['data']) && is_array($_COOKIE['data']){
echo "data present";
} else{
echo "data is not present";
}
you can also check if a variable exist and not empty with empty() function like this :
if(!empty($_COOKIE['data']) && is_array($_COOKIE['data']){
echo "data present and it's not empty";
} else{
echo "data is not present";
}
empty values are : null, "", 0, "0", 0.0, false, [], $var// undeclared var
also i see that your storing an array in the cookie, i suggest for best practice serializing the array before storing in a cookie like this :
setcookie('data', serialize($data), time()+3600);
to get it's value all you have to do is :
$data = !empty($_COOKIE['data']) ? unserialize($_COOKIE['data']) : null;
Related
Guys I want to echo that when I get
as response 0: 'YOU ARE NOT BANNED'
response 1: 'YOU ARE BANNED'
info:
I get the response in form of JSON
state : 0 OR 1
0 for not banned and 1 for banned
This is how the problem looks like:
<?php if (strstr($characters['message']['state']) == 1) {
echo "YOU ARE BANNED";
} else if (strstr($characters['message']['state']) == 0) {
echo "YOU ARE NOT BANNED";
};
I always get YOU ARE NOT BANNED even if the response from JSON is 1
I use the first <?php because it is inside a HTML(which is inside a php file)...
You don't need to call strstr(), just compare the variable directly. And if it's just a 1 or 0, those can be treated as booleans, so you don't even need a comparison.
if ($characters['message']['state']) {
echo "You are banned";
} else {
echo "You are not banned";
}
I want to check if the data is match between array and the string.
Im trying to check if the string is equals to each other but the problem is the condition is returning false and both value of $subex2[1] and $subdat is IT100. I think the problem is you can't compare an array to a string. Can someone help me about this?
here's the code
$subex = $objPHPExcel->getActiveSheet()->getCell('D8')->getValue();
$subex2 = explode(":", $subex);
$q = "select * from class where id='$id'";
$r = mysql_query($q);
$data = mysql_fetch_array($r);
$subdat = $data['subject'];
if($subdat == $subex2[1]) {
echo "Data matched";
}else {
echo "Data doesn't matched";
}
As mentioned in the comments. One value has an space before it. You can solve this kind of problems like this:
if(trim($subdat) == trim($subex2[1])) {
echo "Data matched";
} else {
echo "Data doesn't matched";
}
for case sensitive issue, this trick should apply.
if(strtolower(trim($subdat)) == strtolower(trim($subex2[1]))) {
echo "Data matched";
} else {
echo "Data doesn't matched";
}
this test works fine
$subdat = "IT100";
$subex2[1] = "IT100";
if($subdat == $subex2[1]) {
echo "Data matched";
}
You are not comparing same string
You first use array_map and trim values, delete space, next use in_array, example
$subex2 = array_map('trim',$subex2);
if( is_array($subex2) ){
if(in_array($subdata, $subex2)){
echo "Data matched";
} else {
echo "Data doesn't matched";
}
}
It's always good to check if it's actually an array, with is_array
Reference in_array https://www.w3schools.com/php/func_array_in_array.asp
try this:
$isMatched = strval($subex2[1]) === strval($subdat) ?: false;
So I am trying to hide or show options if the user is logged in or not.
I have this simple statement, its always showing no regardless if I am logged in or not.
if( !isset($_SESSION) ){
echo "yes";
}
else {
echo "no";
}
I have also tried
function __construct()
{
parent::__construct();
$this->is_logged_in();
}
if(is_logged_in())
{
echo "yes";
}
else
{
echo "no";
}
Neither works, I also think the first one if simpler, but I am not sure what method would be better.
isset($_SESSION) checks if the variable is set or not and here $_SESSION is already defined.
so in your case !(isset($_SESSION)) is false coz isset($_SESSION) is true and !true is false
To check for the session value try isset($_SESSION['key_you_set']). This will check if the key_you_set exists or not.
Assuming you have set an session with name session_id
You can retrieve your session information in codeigniter like,
$session_id = $this->session->userdata('session_id');
Now You can check like below
if($session_id==""){
echo "session not set";
}
else{
echo "session set";
}
I think this helps you ..
$session_id = $this->session->userdata('session_id');
less code but more secure
echo (!empty($session_id ) && isset($session_id) ) ? "session set" : "session not set" ; // ternary operator
i need to use if statement inside my table
the value of the row is "Released" but i want to show Pending on my html table. how can i achieve it. this is my current code. what's wrong with my if statement?
if (strlen($row['signed']) == "Released") {
echo "Pending";
}
else{
echo $row['signed'];
}
strlen checks for string length. first check either signed is set in the array and then check if value is equal
if (isset($row['signed']) && $row['signed'] == "Released") {
echo "Pending";
}
else{
echo $row['signed'];
}
strlen() returns the length of the argument, so it returns an integer. You can check if value is equals to the string which you want something like this:
if ($row['signed'] == "Released") {
echo "Pending";
} else {
echo "Released";
}
strlen() is used to count the length of the string, to check if it matches "Released", just use == to compare:
if ($row['signed'] == "Released") {
echo "Pending";
} else {
echo $row['signed'];
}
To find out is $row['signed'] is set, just use isset():
if (isset($row['signed'])) {
echo "Pending";
} else {
echo $row['signed'];
}
More information on isset(): http://php.net/manual/en/function.isset.php
More information on PHP operators: http://php.net/manual/en/language.operators.php
Try this:
if ($row['signed'] == "Released") {
$value = "Pending";
} else {
$value = "Released"
}
And add <?php echo $value; ?> in your table
in if condition i want to check
if(isset($_GET['q']))
{
echo "ok";
}
esle
{
echo "not ok";
}
when $_GET['q']=0 if send me in else part.
But i want to go in if .
if $_GET['q'] have any value even for 0 if should print ok
any help pls ?
This is what isset does. Try:
$x["q"] = 0;
var_dump(isset($x["q"]));
You will get true. If you think isset() returns false on 0 you are looking at the wrong place, look for a bug elsewhere.
0 is not null http://php.net/manual/en/function.isset.php
You might need something like this considering the value you want is integer
if(isset($_GET['q']) && intval($_GET['q']) > 0 )
{
echo "ok";
}
else
{
echo "not ok";
}
perhaps array_key_exists() would be more appropriate.
if ( array_key_exists( 'q', $_GET ) ) ...
I think this is the correct one...
if(array_key_exists('q', $_GET)){
echo "ok";
}
else{
echo "not ok";
}