+ 1

Return in php

I have some ambiguities regarding return in php. How can I return more than one value and store it in different variables like we do in python. Example: I have to create a function whose name is operations, this function takes two parameters that are $num1 and $num2. The function have to retun sum, subtraction, product and division of two numbers. I don't want to create an array. I want to store sum in $sum, subtraction in $sub, product in $prod, and division of two numbers in $div Thanks

26th Jul 2023, 4:16 PM
Khan
12 Réponses
+ 6
You can use array destructuring (from php 7) function test($a){ return [$a+10, $a*10]; } [$sum, $mul]= test(5); echo($sum); // 15 echo($mul); //50 read here for more info https://www.php.net/manual/en/language.types.array.php
26th Jul 2023, 5:55 PM
KrOW
KrOW - avatar
+ 3
Khan If you dont want use an object as return value or use references, yes, you have to use the array in return.. Anyway, why seem that you dont want use the array? Some particular reason?
26th Jul 2023, 8:36 PM
KrOW
KrOW - avatar
+ 2
You can ask your doubt in your code in Q&A discussion forum if you think we can write the code for you? So, the answer is no, because this is not a platform to give you a readymade code, share your attempt firstly & Sololearn is self-learning platform you can do the course from Sololearn and if you have doubt on particular topic then go to community section -> use search bar and you can take the help from internet also, please try to understand and hope you don't mind. https://www.sololearn.com/learn/PHP/1835/?ref=app https://www.tutorialspoint.com/php/php_functions.htm
26th Jul 2023, 4:24 PM
Sakshi [Offline 🙃]
Sakshi [Offline 🙃] - avatar
+ 2
Khan With array destructuring you return an array but it will be "splitted" onto appropriated variables.. Anyway, you are welcome
26th Jul 2023, 9:28 PM
KrOW
KrOW - avatar
+ 2
Sakshi , No no, actually I am new here, don't know enough rules. And Thanks for you too, you told me the ways where I can take help. And my comment was not to make you feel bad. I just explained that others don't get confused. Really Sorry, and Thanks
27th Jul 2023, 6:55 AM
Khan
+ 2
Khan no worry bro, it's ok 👍 Happy coding 😊
27th Jul 2023, 2:18 PM
Sakshi [Offline 🙃]
Sakshi [Offline 🙃] - avatar
+ 2
https://www.w3schools.com/php/php_superglobals_globals.asp you can use $GLOBALS, but I would not recommend it. Unless you have complete control of your codebase, messing with global variables is not a good idea. Array is the better option. <?php $sum = 0; $prod = 0; $quot = 0; $diff = 0; echo "before foo(1, 2):<br>"; echo "\$sum = ".$sum."<br>" ."\$prod = ".$prod."<br>" ."\$quot = ".$quot."<br>" ."\$diff &nbsp;&nbsp= " .$diff."<br>"; function foo($a, $b){ $GLOBALS['sum'] = $a + $b; $GLOBALS['prod'] = $a * $b; $GLOBALS['quot'] = $a / $b; $GLOBALS['diff'] = $a - $b; } foo(1,2); echo "<br>after foo(1, 2):<br>"; echo "\$sum = ".$sum."<br>" ."\$prod = ".$prod."<br>" ."\$quot = ".$quot."<br>" ."\$diff &nbsp;&nbsp= " .$diff."<br>"; ?>
28th Jul 2023, 3:31 AM
Bob_Li
Bob_Li - avatar
+ 2
Bob_Li , Thanks, I'll learn more about it.
29th Jul 2023, 10:12 PM
Khan
+ 1
KrOW , No actually there is no particular reason. I am a beginner and learnt C++, python before. And in python, we don't need to create an array to return multiple values. In sololearn PHP course, somone in comments told that we can return multiple values by creating array. I tried it, but don't know about array deconstruction. So, I was curious how to store them, is there any alternative way? Bcz there are many ways to solve a problem. Once again, I want to thanks a lot. The link you provided me is best, that's all what I needed. Special thanks
26th Jul 2023, 8:48 PM
Khan
+ 1
Khan carefully see your question, you said tell me return with 2 variables through example and here we can't write code because it's rules until the opponent himself does not send the code, Sir has also provided you the link, I have also given the link of other website along with the particular topic, but that's ok if you see my mistake and sorry for that, It's good that your problem is solved 👍
27th Jul 2023, 2:59 AM
Sakshi [Offline 🙃]
Sakshi [Offline 🙃] - avatar
0
Sakshi , Sorry, but I think you misunderstood me. I don't need the code, i just told you the condition, if you want me to write code for that condition, I will write. I just gave an example. Like if we have to return more than one items from function how we will do without returning an array? Bcz when I tried, it was not accepting the below statement return $sum, $sub;
26th Jul 2023, 8:27 PM
Khan
0
KrOW Thanks, it means I have to use array.? There is no alternative way right?
26th Jul 2023, 8:32 PM
Khan