[Array] Where is the error?-php Tutorial

Source: Internet
Author: User
[Array] What is the error?
 "Reunion", "07-21" => "credit and loan repayment", "07-23" => "purchasing products"); $ date = "07-23 "; foreach ($ rc as $ key => $ value) {if ($ key = $ date) {echo "script alert ('". $ key. "Memo :". $ value. "')" script "; break;} else {echo" "script" alert ('No memos') "script"; break ;}}?>


If the third line is: $ date = "07-12", a prompt box is displayed: 07-12 memo: Reunion
If the third line is: $ date = "07-21", or: $ date = "07-23", it should be the same as above, but the prompt box is displayed: no memo
Where is the error?


Reply to discussion (solution)

Do you have a break? the traversal will exit once.

On the same floor, line 3 cannot break

$ Rc = array ("07-12" => "reunion", "07-21" => "credit repayment ", "07-23" => "buy item"); $ date = "07-23"; $ msg = ''; foreach ($ rc as $ key => $ value) {if ($ key = $ date) {$ msg = "script alert ('". $ key. "Memo :". $ value. "') script"; break;} if (! $ Msg) $ msg = "script" alert ('No memos ') script "; echo $ msg;
Think about why

$ Rc = array ("07-12" => "reunion", "07-21" => "credit repayment ", "07-23" => "buy item"); $ date = "07-23"; $ msg = ''; foreach ($ rc as $ key => $ value) {if ($ key = $ date) {$ msg = "script alert ('". $ key. "Memo :". $ value. "') script"; break;} if (! $ Msg) $ msg = "script" alert ('No memos ') script "; echo $ msg;
Think about why

Positive solution. Thank you.

But why is it written like this? I haven't figured it out yet.

Do you understand this?

$ Rc = array ("07-12" => "reunion", "07-21" => "credit repayment ", "07-23" => "purchasing products"); $ date = "07-23"; $ msg = "script" alert ('No resilies ') script "; // give an initial value foreach ($ rc as $ key => $ value) {if ($ key = $ date) {// If yes, modify $ msg = "script alert ('". $ key. "Memo :". $ value. "') script"; break; // just to end the loop in advance} echo $ msg;

Xuzuning is easy to use, and the code on the 6th floor is easy to understand.
I used netbeans-xdebug to run the code on the first floor. I checked the variable value changes in one step and understood the error of the code on the first floor.
First floor code if the third line is: $ date = "07-21"; the first cycle, $ key is 07-12, not equal to $ date07-21, in this case, the js prompt box is "no memo" instead of "-21: Credit repayment ".
The correct method is that a prompt box is displayed only when $ key = $ date.
In this case, there cannot be two echo sets in the next loop.

A basic problem. if you don't pay attention to it, it will actually go around!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.