简体   繁体   中英

Echoing the return value of a function in PHP

For some weird reason, my echo function isn't outputting anything. The code is very simple, therefore I just don't see why it wouldn't output correctly.

    <h2>First function:</h2><br>

<?php 
    function splitSort($String) {
     $splitString = explode (" " , $String);
        sort($splitString);
        return $splitString;
}
    ?>
    <div>
    <?php

    $question1 = $splitSort("Dog Pig Cat Mouse Cattle");
    echo 'Example of Question 1 for string "Dog Pig Cat Mouse Cattle", the method returns :' . $question1; 

        ?>
    </div>

I have fixed the erros I found, this is the correct code. Note: the variable $question1 is an array , I have printed out the first element of the array for the purpose of this example.

<h2>First function:</h2><br>
<?php 
function splitSort($String) {
    $splitString = explode (" " , $String);
    sort($splitString);
    return $splitString;
}
?>
<div>
<?php
    $question1 = splitSort("Dog Pig Cat Mouse Cattle");
    echo 'Example of Question 1 for string "Dog Pig Cat Mouse Cattle", the method returns :' . $question1[0]; 
    //question1 is an array containing: "Dog, Pig, Cat, Mouse, Cattle". With [0], like the example you will get Dog.
?>
</div>

In case you still don't get anything, any of these (or more) could be your answer why it is not working

  1. Is there actually PHP running on your computer?
  2. Is the file extension .php?
  3. Are you accesing the file through your browser doing something like http://localhost/myfile.php
  4. If it is on a remote server, is there PHP installed?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM