0

Hello I have a problem with this function I want to show the result of the function to put in a variable and send it to the database but it does not show me anything you can see in the example in the Second Block when I added echo its show me but I don't know how to get that result out I did use return but it gave a different result

$fullname = "Ayoub Chafik" ;

function orderID($data){
    $AKK = "AK". date('YmdHis');
    $string = strtoupper($data);
    $strs=explode(" ",$string);
    foreach($strs as $str)
    $str[0];

}
// I want to see resulte here of this function
   echo orderID($fullname) ;


?>

This is the second Block

<?php

$fullname = "Ayoub Chafik" ;

function orderID($data){
    echo $AKK = "AK". date('YmdHis');
    $string = strtoupper($data);
    $strs=explode(" ",$string);
    foreach($strs as $str)
    echo $str[0];

}

echo orderID($fullname) ;


?>
6
  • Add return $str[0]; at the end of the function Commented Nov 14, 2017 at 3:07
  • Your $str[0] is under foreach loop, it return the first element value. It won't continue the loop. You can create variable and store the values & at the end return the new varaible Commented Nov 14, 2017 at 3:09
  • when I do it, it changes the value of the variable $str[0] that is AC to 0 Commented Nov 14, 2017 at 3:10
  • What are you trying to do ? Split the names by first and last then save into database? Commented Nov 14, 2017 at 3:12
  • @giolliano yeah split the first letter of the first name and the last name + a code and save them in the database Commented Nov 14, 2017 at 3:16

2 Answers 2

1

As I explained in comment you store it in a variable and return it. Check below example. Also it is a good practice to open the {} even its only one line for more readability.

$fullname = "Ayoub Chafik" ;

function orderID($data) {
    $string  = strtoupper($data);
    $strs    = explode(" ", $string);
    $results = '';

    foreach($strs as $str) {
        $results .= $str[0];
    }

    return $results;
}

echo orderID($fullname) ;
Sign up to request clarification or add additional context in comments.

2 Comments

can explain to me if you have time the dot. before the = sign role in the function ?
It is similar to $results = $results . $str[0]. String concatenation.
1

Some examples varying output based on your code sample.

$fullname = "Ayoub Chafik";
function orderID($data){
    $string = strtoupper($data);
    $strs=explode(" ",$string);
    foreach($strs as $str) {
    return $str;
    }
}
echo orderID($fullname) ;
// output "AYOUB"
function _orderID($data){
    $return = false;
    $string = strtoupper($data);
    $strs=explode(" ",$string);
    foreach($strs as $str) {
    $return = $str;
    }
    return $return;
}
echo _orderID($fullname) ;
// output "CHAFIK"
function _orderID_($data){
    $return = [];
    $string = strtoupper($data);
    $strs=explode(" ",$string);
    foreach($strs as $str) {
    array_push($return,$str);
    }
    return json_encode($return);
}
echo _orderID_($fullname) ;
// output ["AYOUB","CHAFIK"]

check the above examples on PHP SandBox

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.