Do i need to declare an array if i want to assign another array to it?
I have a small question. Do i need to declare an array if i want to assing another array to it?
I want to validate the values from a multiple select control, and i need them to be in an array (for sticky form fields)... i don't re开发者_运维问答member if in php, i can just assign the select array to a var and the var will become an array automatically or the var needs to be declare first as an array...
I've tried both methods, and it looks to me that both work ok. But, for good practices, i want to make sure... i need to know from the experts!
What do you think?
<select name="genre[]" multiple="multiple" id="genre">
<option value="...">value goes here</option>
</select>
// genres
if (isset($_POST['genre'])) {
$selected_genre = $_POST['genre'];
}
or
// genres
$selected_genre = array();
if (isset($_POST['genre'])) {
$selected_genre = $_POST['genre'];
}
Quick answer, when you set a variables value as an array, it is an array. Longer answer in safe_mode you get warnings for undefined indexes, and in OOP (object oriented programming) in some cases yes. And generally it is better practice to define variabled before use
Technically, no. However, it makes it easier later, so you don't need is_array
calls to verify that there's something assigned.
精彩评论