error on form submit if no second parameter - php

When i submit my form and if the second parameter is empty.
It throws error.
Message: in_array() expects parameter 2 to be array, null given
Filename: user/Users_groups.php Line Number: 250
I know there is no second parameter. $this->session->userdata('modify')
if (!in_array('user/users_groups', $this->session->userdata('modify'))) {
$this->error['warning'] = 'You do not have permission to modify';
}
Is there away to make it stop throwing that error when user or I try to submit form if no second parameter $this->session->userdata('modify')
But still have that code.

Try to check first if parameters are valid:
<?php
if(is_array($this->session->userdata('modify')) && !empty($this->session->userdata('modify')))
{
if (!in_array('user/users_groups', $this->session->userdata('modify')))
{
$this->error['warning'] = 'You do not have permission to modify';
}
}else{/* handle errors */}
?>

Replace the null value with an empty array before the if statement:
Could use a ternary operator like this:
$array = (is_array($this->session->userdata('modify')))
? $this->session->userdata('modify')
: Array()
It will use the 'modify' array if it's an array, else will pass an empty array

I would also check array_search() function because it can be compared with NULL wich is returned if irregular parameters set.
5.3.0 As with all internal PHP functions as of 5.3.0, array_search() returns NULL if invalid parameters are passed to it.

Related

implode() :invalid argument passed

function submit_data()
{
$st_value='';
$ft_value='';
$mt_value='';
$otr_value='';
$st_details= $this->input->post('check_list');
$ft_details= $this->input->post('ft_check_list');
$mt_details= $this->input->post('mt_check_list');
$otr_details= $this->input->post('otr_check_list');
//print_r($st_details);
$st_value=implode(",",$st_details);
$ft_value=implode(",",$ft_details);
$mt_value=implode(",",$mt_details);
$otr_value=implode(",",$otr_details);
$index= $this->register->insert_details($st_value,$ft_value,$mt_value,$otr_value);
//$this->register->update_details($st_value,$ft_value,$mt_value,$otr_value);
$this->session->set_flashdata('success_message',$success_message);
redirect(base_url().'new_register/index/'.$index);
}
Here is my controller function and i am getting an error message of implode(): invalid argument passed while submiting,How can i intercept the error.
implode() method accept second parameter as array. I think you are providing it a string.
Check by var_dump($st_details);
As others said, implode() uses second parameter as array. You can test your varriable to see if it's an array using is_array() or using var_dump() to see its details.
implode()
is_array()
var_dump()

php Code Array boolean returned

So I'm using a checkbox field, and I check it's value by using the code below and print things out accordingly. Anyway, if the field's checkboxes don't have any value, meaning all of them are unchecked, I get an error.
Warning: in_array() expects parameter 2 to be array, boolean given in /filepath.php on line 647
<?php if (in_array( 'Subbed', get_field('episode_sdversion'))) { ?>
Subbed
<?php } else {
echo '--';
} ?>
So basically, what can I do with this code to make it so when all values are unchecked, that would automatically mean that "Subbed" value is also unchecked, so it should simply just show echo '--';. So how can I make this echo '--'; run when all values are unchecked. So it shouldn't come up with that error?
I'm not sure what your get_field() function does, presumably it's part of a framework or something, but I'm guessing it returns the value of $_REQUEST['episode_sdversion'], which will be FALSE when the checkbox is empty.
In this case, if I understand your question correctly, a simple check to first see if get_field() is returning something other than FALSE would suffice:
<?php if (get_field('episode_sdversion') && in_array('Subbed', get_field('episode_sdversion'))) { ?>
Subbed
<?php } else {
echo '--';
} ?>
You are getting the error because get_field returns false instead of an array when no boxes are checked. The && operator is short circuit, meaning that if the first part gets evaluated as false, the second part won't get executed. So you can avoid the error by replacing your first line (if in_array(...)) with
if(get_field('episode_sdversion) && in_array('Subbed', get_field('episode_sdversion')))
You either have to change that code, or the get_field return value. A way to do it, would be to declare an array() in all cases, and then add every posted checkbox, so that you always have an array passed as the second parameter of the in_array function.

php check empty array

after submited form i want to check array if array is empty alert error for user. but i get error when submited form:
PHP
$errors = array_filter($_POST['session']);
if (!empty($errors)) {
foreach ($_POST['session'] as $value) {
$session.=$value.',';
}
$session=substr($session, 0 , -1);
}
Warning: array_filter() expects parameter 1 to be array, null given in C:\inetpub\wwwroot\manage\test_bank\index.php on line 729
You need to check wheather it is an array or not, before doing any array operation.
if(is_array($_POST['session'])){
$errors = array_filter($_POST['session']);
}
The warning occurs because array_filters() requires an array to be passed to it. Before passing $_POST['session'] to this function, very if that it is an array:
if(is_array($_POST['session'])) {
$errors = array_filter($_POST['session']);
// continue on
}
use is_arrayfor checking weather it is array or not.
echo is_array($_POST['session']);
This is because $_POST is not an array I guess you are looking for this :
$errors = array_filter($_POST);
The following would most simply check for empty errors or not
!empty($_POST['session'])
Would work provided you are not stuffing empty entries in the $_POST['session'] in no error cases. Why do you need the array_filter?
$_POST is an array, but here $_POST['session'] is not.
You can smply try this:
if(isset($_POST['session']))
{
//do your stuff
}
Change it to array_filter($_POST) because $_POST is an assoc array, or check if $_POST['session'] is an array using this line is_array($_POST['session']) before the array_filter().
You should check first if the variable you are working with is an array before using array functions.

How to check if a PHP array has any value set?

I am working on a signup form, I am using PHP and on my processing part I run some code, if a submitted item fails I then add it to an errors array.
Below is a snip of the code, I am at the point where I need to find the best method to determine if I should trigger an error.
So if there is a value set in the error array then I need to redirect and do some other stuff.
I was thinking of using isset or else is_array but I don't think that is the answer since I set the array using **$signup_errors = array()** wouldn't this make the is_array be true?
Can anyone suggest a good way to do this?
//at the beginning I set the error array
$signup_errors = array();
// I then add items to the error array as needed like this...
$signup_errors['captcha'] = 'Please Enter the Correct Security Code';
if ($signup_errors) {
// there was an error
} else {
// there wasn't
}
How does it work? When converting to boolean, an empty array converts to false. Every other array converts to true. From the PHP manual:
Converting to boolean
To explicitly convert a value to
boolean, use the (bool) or (boolean)
casts. However, in most cases the cast
is unncecessary, since a value will be
automatically converted if an
operator, function or control
structure requires a boolean argument.
See also Type Juggling.
When converting to boolean, the
following values are considered FALSE:
the boolean FALSE itself
the integer 0 (zero)
the float 0.0 (zero)
the empty string, and the string "0"
an array with zero elements
an object with zero member variables (PHP 4 only)
the special type NULL (including unset variables)
SimpleXML objects created from empty tags
Every other value is considered TRUE (including any resource).
You could also use empty() as it has similar semantics.
Perhaps empty()?
From Docs:
Return Values
Returns FALSE if var has a non-empty
and non-zero value.
The following things are considered to
be empty:
"" (an empty string)
0 (0 as an integer)
"0" (0 as a string)
NULL
FALSE
array() (an empty array)
var $var; (a variable declared, but without a value in a class)
Check if...
if(count($array) > 0) { ... }
...if it is, then at least one key-value pair is set.
Alternatively, check if the array is not empty():
if(!empty($array)) { ... }
Use array_filter if you already have keys, but want to check for non-boolean evaluated values.
<?php
$errors = ['foo' => '', 'bar' => null];
var_dump(array_filter($errors));
$errors = ['foo' => 'Oops', 'bar' => null];
var_dump(array_filter($errors));
Output:
array(0) {
}
array(1) {
["foo"]=>
string(4) "Oops"
}
Use:
<?php
if(array_filter($errors)) {
// Has errors
}
You could check on both the minimum and maximum values of the array, in this case you can have a large array filled with keys and empty values and you don't have to iterate through every key-value pair
if(!min($array) && !max($array)) { ... }
The language construct isset(), is for testing to see if variables and array elements are set and not NULL. Using is_array() would tell you if the argument you supply to it is an array. Thus, I do not think using isset() or is_array() would give you the correct and desired result that you are seeking.
The code:
$signup_errors = array();
means that ...
is_array($signup_errors);
would return true. However, this does not mean that the Boolean language rules of PHP would evaluate....
if($signup_errors)
{
//*Do something if $signup_errors evaluates to true*;
}
as true, unless some elements are added to it. When you did this,
$signup_errors['captcha'] = 'Please Enter the Correct Security Code';
you fulfilled the PHP language requirement for the array above to evaluate to true.
Now, if for some reason you wanted, or needed, to use isset() on the array elements in the future, you could. But, the conditional statement above is enough for you this case.
I should add an obvious answer here. If you initialise your error array as an empty array. And later want to check if it is no longer an empty array:
<?php
$errors = [];
if($errors !== [])
{
// We have errors.
}

PHP error Can't use method return > value in write context

I am getting this error in a PHP class...
Fatal error: Can't use method return
value in write context in
C:\webserver\htdocs\friendproject2\includes\classes\User.class.php
on line 35
Here is the troubled part.
if(isset($this->session->get('user_id')) && $this->session->get('user_id') != ''){
//run code
}
This code is in my contrustor, is a value is not already set for $this->session->get('user_id') then it will return false instead of a Number. So as you can see I was hoping to check if this value is a number or not or not even set.
Any help with fixing appreciated.
You can't use isset for the result of a function. Consider the following code instead:
if( $this->session->get('user_id') ){
//run code
}
isset() only works with variables as
passing anything else will result in a
parse error. For checking if constants
are set use the defined() function.
From the PHP Manual.
You can't use isset on a function. However, since false, 0, and '' all equate to a falsey statement, write your test this way:
if( $id = $this->sessions->get('user_id') ){
// Will only run if $id does not equal '', False, or 0
}
That way you have run your test and assigned the variable in one step.

Categories