Personally I find this to be the most clean – not sure if it’s the most efficient, mind!
if (is_array($values) || is_object($values)) { foreach ($values as $value) { ... } }
The reason for my preference is it doesn’t allocate an empty array when you’ve got nothing to begin with anyway.
Related Posts:
- Difference between “as $key => $value” and “as $value” in PHP foreach
- break out of if and foreach
- Trying to get property of non-object in
- php foreach with multidimensional array
- Find the last element of an array while using a foreach loop in PHP
- How do I get a YouTube video thumbnail from the YouTube API?
- How to avoid Request Entity Too Large 413 error
- Array to String PHP?
- Should I use mysqli_real_escape string() or mysql_real_escape_string() for form data?
- How to fix “Headers already sent” error in PHP
- PDOException SQLSTATE[HY000] [2002] No such file or directory
- Form submission: PHP S_SESSION statements within a foreach loop
- What are the main differences between PHPExcel and PhpSpreadsheet?
- PDOException: SQLSTATE[HY000] [2002] No such file or directory
- How do I get a file extension in PHP?
- mysqli_fetch_array() expects parameter 1 to be mysqli_result, boolean given in
- Convert a PHP object to an associative array
- XAMPP Port 80 in use by “Unable to open process” with PID 4
- How to parse a CSV file using PHP
- Cannot use object of type stdClass as array?
- Is a new line = \n OR \r\n?
- Regex to check for new line
- Email Not Sending from Byethost Hosting
- Redirect vs RedirectMatch
- what does a .php?id=&value=value means?
- Setting up a PHP interpreter in PhpStorm
- Can curl make a connection to any TCP ports, not just HTTP/HTTPS?
- Get Current URL in Magento and show something
- Error 330 (net::ERR_CONTENT_DECODING_FAILED):
- What does __FILE__ mean?
- Fatal error: Call to a member function bind_param() on boolean [duplicate]
- How do I fix “Undefined variable” error in PHP?
- How do you parse and process HTML/XML in PHP?
- Is there a difference between the UTC and Etc/UTC time zones?
- Fatal error: Call to a member function prepare() on null
- PHP mail not working for some reason
- move_uploaded_file gives “failed to open stream: Permission denied” error
- PHP convert XML to JSON
- PHP if not statements
- PHP with MySQL 8.0+ error: The server requested authentication method unknown to the client
- WordPress Error : Upload folder is not writable. Export and file upload features will not be functional
- How to call function of one php file from another php file and pass parameters to it?
- how to get id from URL using php
- Fatal error: Cannot use object of type mysqli_result
- mysql_field_name to the new mysqli
- MAMP “Apache couldn’t be started because port is in use.” AND “Can’t connect to local MySQL server through /tmp/mysql.sock
- mysqli_select_db() expects parameter 1 to be mysqli, string given
- Php include not working? function not being included
- PHP mail function doesn’t complete sending of e-mail
- Remove all special characters from a string
- Convert php array to Javascript
- how to convert object into string in php
- PHP Fatal error: Call to undefined function mssql_connect()
- phpMyAdmin ERROR: mysqli_real_connect(): (HY000/1045): Access denied for user ‘pma’@’localhost’ (using password: NO)
- Call to a member function on null?
- how to ping ip addresses in php and give results
- do_shortcode not working
- SQLSTATE[HY093]: Invalid parameter number: number of bound variables does not match number of tokens on line 102
- Only variable references should be returned by reference – Codeigniter
- PHP Unset Session Variable
- “Fatal error: Cannot redeclare function”
- php get all url variables
- How to remove unused dependencies from composer?
- how to get date of yesterday using php?
- phpinfo() is not working on my CentOS server
- How can I fetch all items from a DynamoDB table without specifying the primary key?
- ERROR: SQLSTATE[HY000] [2002] No connection could be made because the target machine actively refused it
- How to extract and access data from JSON with PHP?
- How to convert array to SimpleXML
- What does double question mark (??) operator mean in PHP
- How can I find the php.ini file used by the command line?
- WordPress query single post by slug
- TCPDF ERROR: Some data has already been output, can’t send PDF file
- Check if a PHP cookie exists and if not set its value
- Deprecated: mysql_query() [duplicate]
- Limit String Length
- How to run PHP locally on MacOS
- Call PHP function from jQuery?
- PhpMailer SMTP NOTICE: EOF caught while checking if connected
- Facebook – Error parsing input URL, no data was cached, or no data was scraped
- What is the meaning of sprintf(): Too few arguments
- How to use $_SERVER[‘HTTP_REFERER’] correctly in php?
- what is $_SERVER[‘QUERY_STRING’] ? how it works?
- PHP Error : Fatal error: Constant expression contains invalid operations
- JavaScript equivalent of PHP’s in_array()
- Paypal IPN Verification Postback with HTTPS
- Create a folder if it doesn’t already exist
- Implement linked list in php
- Using PHP 7 with WAMP
- Warning: Unknown: failed to open stream: No such file or directory in Unknown on line 0
- PHP: date function to get month of the current date
- Warning: mysqli_real_escape_string() expects exactly 2 parameters, 1 given… what I do wrong?
- PHP: bootstrap.php
- Display array values in PHP
- Php create a file if not exists
- Page redirect after certain time PHP
- Check if $_POST exists
- PHP & Case Sensitivity
- PHP Header redirect not working [duplicate]
- PHP – Fatal error: Unsupported operand types