[PHP] foreach, trinomial calculus

Sep 5, 2020 PHP

I will summarize what I learned about PHP as a memorandum.

#foreach Loop processing specialized for arrays. Iterates as much as the elements of the array are included.

For example, the execution example is as follows.


<? php
$ test = array ("test1", "test2", "test3");
foreach ($ test as $ value) {
print $ value. "<br>";
}
?>

#Execution result
test1
test2
test3

In this way, the three elements were iteratively processed.

##Associative array The same processing can be performed with an associative array, It works as follows.


<? php
$ test = array (“test1” => “test01”, ”test2” => “test02”, ”test3” => “test03”);
foreach ($ test as $ key => $ value) {
print $ value. ”Is“. $ Key. <br>;
}
?>

#Execution result
test1 is test01
test2 is test02
test3 is test03

#Ternary operator Take three operands (operands) that are the operation targets, and? And: is used in the following format. The following is an example of executing the ternary operator.


$ val = 120;
$ result = $ val> 100? '100 greater than': '100 or less';
print $ result;

The ternary operator mentioned earlier can be expressed by an if statement as follows.


$ val = 120;
if ($ val> 100) {
print '100 or greater';
} else {
print '100 or less';
}

#Reference URL https://blog-and-destroy.com/7118 https://techacademy.jp/magazine/4978