I've got several foreach loops in one of my scripts. For example: foreach($_POST['tags'] as $index1 => $value1) Now whenever the $_POST value does not exist I get the warning message on the page. Now I know I can get around this by putting the foreach loop inside an IF statement with an isset command. But I was wondering if there is a better way to do this - can the $_POST value be initialized in the foreach statement? Or can the isset command be integrated in to the foreach statment?
<?php error_reporting( E_ALL ); foreach ( (isset($_POST['var']) && is_array($_POST['var']) && count($_POST['var']) > 0) ? $_POST['var'] : array() as $key => $value ) { /* Do something. */ } PHP:
You can use the IF statement without the isset command. This might speed up processing a little: if ($_POST['var']) { stuff... } It'll just validate if the variable exists. Usage in this fashion will depend upon how your php.ini is configured.
Not recommended, it will trigger a warning if $_POST['var'] is not set, and it will not run if it equates to '', 0 or similar. Dan.
You're right. Like I said, it depends on how your php.ini is configured. Whether or not a warning is logged will depend on your log level. You are also right about sending a 0 as the variable. However, I do feel that in certain contexts this method is acceptable and quicker.
I would just add the if statement before the foreach loop. The code is much cleaner and easier to read that way.
In the last bit of code on this php mail() tutorial, it uses isset within an if/else statement, similar to what phper suggested: hxxp://www.jwrmedia.com/lessons/php/mail
Same here. That's really what if statements are for. There's no need to be clever for the sake of being clever.
You can typecast it to an array inline and reduce it to an E_NOTICE for the undefined index which should be ignored by all but the strictest setups. foreach((array)$_POST['var'] as $key => $val) Code (markup):
I assuming that your form action url is same as the script itself. You can stop the script if there is no post data. if (empty($_POST)) exit; PHP:
Wow, that's brilliant, just what I needed. It's a lot better than having to put each of the foreach loops in to an if(isset) statement!
E_NOTICES means you're doing it wrong. Good code show have no errors whatsoever, even with error_reporting set to E_ALL; It's really is worth writing code so it works properly first time, helps portability for starters... Dan.
Never turn E_NOTICES off. As mentioned above it will make your code unstable as well as less secure. Bugs can turn into major exploits and your site will get hacked in no time unfortunately. Regards, Dennis M.
if (isset($_POST['tags']) && is_array($_POST['tags'])) { foreach($_POST['tags'] as $index1 => $value1) { // do something } } Code (markup):
Thanks for the input guys. I didn't know about E_ALL and E_NOTICE until now. I thought my code was perfect until I switched these on, now I'm getting all sorts of undefined index errors! For example: <input type="text" id="', $key ,'" name="', $key ,'" value="', $_POST[$key], '" size="30"' ,'/> The error occurs on the value attribute on first load of the page, as there is no $_POST[$key] variable set. What is the easiest way to rectify this?
Cheers mate. I've got another one which is erroring, this one is a little trickier though: $value[2] == $_POST['region_name'] ? ' selected="selected"' : '' ,' PHP: I cant figure out how to put the isset condition in there.
http://www.php.net/manual/en/errorfunc.configuration.php#ini.error-reporting Don't use E_ALL on a server that's accessible to the public. E_NOTICE is omited from the default PHP configuration for good reason.
isset($_POST['region_name'], $value[2]) and $value[2] == $_POST['region_name'] ? ' selected="selected"' : ''
Cheers again Danltn. That didn't quite work so I amended it as such: isset($_POST['region_name']) && $value[2] == $_POST['region_name'] ? ' selected="selected"' : '' I didn't realise you could combine an AND condition with a ternery operator. I'm getting there, slowly but surely... joebert - Thanks for the pointer. I'm currently developing and testing on my localhost server at home, so that's OK. But yeah I would never turn on error notices or output fatals on the live server!