Home > Backend Development > PHP Problem > How to determine if array elements are not spaces in php

How to determine if array elements are not spaces in php

青灯夜游
Release: 2023-03-12 11:06:02
Original
2075 people have browsed it

Methods to determine if array elements are not spaces: 1. Use the "foreach ($arr as $value){}" statement to traverse the array; 2. Use the "if($value!=" ")" statement to determine Whether the array elements are spaces; 3. If all array elements are not empty, output "array elements are not spaces".

How to determine if array elements are not spaces in php

The operating environment of this tutorial: Windows 7 system, PHP version 7.1, DELL G3 computer

php determines that the array element cannot be a space

Idea:

  • Use the foreach statement to traverse the array, and assign the key value of the current array to $value## in each loop

  • #In the loop body, use the if statement to determine whether the array element is a space

    • If it is not a space, then

      $i

    • If it is a space, set

      $i=0, and use break; to exit the entire loop.

  • Use the if statement to determine whether the

    $i value is 0, then you can know whether there is a space element in the array

Implementation code:

<?php
header("Content-type:text/html;charset=utf-8"); 
$arr = array(10,"php中文网",20," ","php教程");
$i=0;
foreach ($arr as $value) { 
    if($value!=" "){
    	$i++;
    }else{
    	$i=0;
		break;
    }
}
if ($i==0) {
	echo("数组中有元素为空格");
} else {
	echo("数组元素都不为空格");
}
?>
Copy after login

Output result:


How to determine if array elements are not spaces in php## Recommended learning: "

PHP video tutorial

The above is the detailed content of How to determine if array elements are not spaces in php. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template