Php function with return statement [closed] - php

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 4 years ago.
Improve this question
Can somebody explain me this code? I don't understand why it outputs 21.
<?php
function math($t){
if($t==0)
return 0;
return $t+ math($t-1);
}
echo math(6);
?>
It will echo 21. I have no idea how it got this result.

The function is recursive, it calls itself until it gets to 0, then adds all the previously returned values (6,5,4,3,2,1).
function math($t){
if($t==0)
return 0;
return $t+ math($t-1);
}
echo math(6);
So on loop one it gets 6 then 6-1 = 5 so math gets called again with 5 this time and so on. Take a look at http://sandbox.onlinephpfunctions.com/code/e228f3b696c5058efee03fa978a09179c1f2ffbb.

Related

How to format a string returned by a PHP function [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I wrote a program that uses an ISBN to query Amazon for that ISBN's price, and returns the following result:
5cb66919-8a8d-4c13-9175-790f0476508d0.0401580000000000TrueISBN9780340979266OffersAllAll0340979267809USD$8.09688USD$6.88598USD$5.98241010000
I need to format this so that I have the price information in a format that would be understandable to a human.
How can I do this?
Use this way..
<?php
$s = '5cb66919-8a8d-4c13-9175-790f0476508d0.0401580000000000TrueISBN9780340979266OffersAllAll0340979267809USD$8.09688USD$6.88598USD$5.982410100000';
$p = explode('USD',$s);
echo "<pre>";
print_r($p);
echo "</pre>";
?>
function returnPrice($response){
$prices = explode('USD',$response);
unset($prices[0]);
return $prices;
}

PHP If Then statement based on numeric range from field output [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I have a number field: <?php echo $entity->field_zip_code_name[0];?>
I want to create an if then statement based on the output number. Here is what I have:
if ($entity->field_areacode[0] = 92919,92923,94975,37783) {
<h3 style="font-size: 20px;"><strong>Area Code:</strong></h3>;
}
else {
NONE;
}
any help would be greatly appreciated.
Not sure what x is, if it's $entity->field_zip_code_name[0] then echo that:
if(in_array($entity->field_zip_code_name[0], [92919,92923,94975,37783])) {
//display x
}
If your PHP version doesn't support [] then use array():
if(in_array($entity->field_zip_code_name[0], array(92919,92923,94975,37783))) {
//display x
}

Access an array index from a function call [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I have an array like
$remarks = array('Poor','Fair','Good');
I want to access the array index by function call in it.
echo $remarks[myindex($id)];
where myindex() is a function which returns some number value from database.
How can I access the array index of my array at run time
As per you say..
myindex is a function which returns a value between 0-2 then I want to
display array value of that index
<?php
$remarks = array('Poor','Fair','Good');
function myIndex()
{
return rand(0,2);
}
echo $remarks[myIndex()]; //"prints" either Poor , Fair or Good randomly..
The myIndex() function returns a random value between 0,1 or 2 , so that is passed as the index value to your array and it prints the values either Poor , Fair or Good.
it may be
function myindex($id){
return $id % 3;
}

Recursive function for pair of numbers [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 9 years ago.
Improve this question
I want to create a recursive function that displays in order n numbers from greatest to 2 that are pair. If n=5 it should display:
10 8 6 4 2
To get the pair number displayed, I am using:
if($i %2==0)
echo $i
to order from the greatest I also use i--. The problem is I don't know how this recursive function really works.
Do something like this...
<?php
$inc=2;
recv(7); // Calling your recursive function i.e. passing the parameter 5 or 7
function recv($v) // Your function definition
{
global $inc;
if($v>=1)
{
echo $inc*$v." ";
$v--;
recv($v); // Function calls itself in other words "recursive call"
}
}
OUTPUT :
14 12 10 8 6 4 2

PHP - Creating custom variable for template [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
I don't know exactly how to say this, I'll use an example.
//[$var:Username|n] will print username with n length
$myText = 'The participants are [$var:Username|10], [$var:Username|8], and [$var:Username|6]';
$username = array('Alexandrite', 'Maximillian', 'Angelina');
$result = someFunction('[$var:Username|n]', $username, $myText);
echo $result;
//The participants are Alexandrit, Maximill, and Angeli
Any idea how to do this? Maybe using preg_replace?
I'm interested to know why you would even need to do this. Regardless:
preg_replace_callback('/\[\$var:Username\|(\d+)\]/', function ($match)
use (&$count, $username
) {
return substr($username[$count++], 0, $match[1]);
}, $myText);

Categories