A small case of proxy function invocation in PHP

Source: Internet
Author: User

Simply write a small example and you'll see.

Example: Enter a string in a text box separated by commas, and then click Submit to automatically remove the comma concatenation together.

Like this

Click Submit past Return 12345

This small example uses the proxy function call, first of all, to say the name Call_user_func_array ();

Call_user_func_array (' function name to invoke ', ' value of call ');

Write the submission form first:

<form method=post action= "test.php" > enter a string separated by commas: <input type= "text" name= "str" ><BR/>< Input type= "Submit" name= "button" value= "Commit" ></form>

Then write the code that accepts the validation:

<?php function content () {$sum =func_get_args (), if (Empty ($sum)) {echo ' Please enter content! '; return;} $num = "; for ($i =0; $i <count ($sum); $i + +) {$num. = $sum [$i];} echo $num;}    if (Isset ($_post[' button ')) {$str =$_post[' str '];    $array =explode (', ', $str); Call_user_func_array (' content ', $array); }
Explanation: 1. First judge the submitted content, and then "," the string into an array ($array) 2. Define a function, get the value of the pass and break it into an array ($sum), return ' Please enter ' if no content is passed, and then make a loop to print a few values from the $sum array and use '. ' Link 3. Because a function cannot be called across a variable field (so to speak, it is understood anyway, so good to understand, do not hit me qaq), so use the proxy function call (Call_user_func_args ()) 4.  Use Call_user_func_array (' content ', $array); Let this function call for us, this code is similar to
function content ($a) {//code block}content ($array);
5. This completes the proxy invocation of a function, which is usually used when we cannot invoke the function we write. 6. What's wrong with the place welcome to point out, anyway, I'm not going to change ~qaq~~qaq~.

A small case of proxy function invocation in PHP

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.