I wanted to have a result like this
50+2+2+2+2 = 58
But I'm getting these kind of result
50
2
2
2
2
these are my codes.
<?php
$height = 50;
echo $height;
function hey($x)
{
return $height += $x;
}
$i = 4;
while($i != 0)
{
echo "<br>".hey(2);
$i--;
}
?>
Please take note that the location of my variable and loop must be and really meant at that position.
what do i need to change on my code. I am new in using php functions.
thanks for the help.
You can use a global variable like this try:
function hey($x)
{
global $height;
return $height += $x;
}
And print the variable height only after the called function.
If you don't put global before the variable inside the function it the seems that you create a new variable inside your function. With global you tell to the server to take the variable that you have created outside the function
In this function:
function hey($x)
{
return $height += $x;
}
$height is not in scope, so it is undefined. You should pass it in:
function hey($x, $height)
{
return $height += $x;
}
Then call it like this:
hey(2, $height);
this is scope problem :
function hey($x)
{
global $height;
return $height += $x;
}
Change to:
global $height;
then
while($i != 0)
{
echo "+".hey(2);
$i--;
}
echo "=" . $height;
I don't understand that you want, but if you need output like that, please try this code..
br mean go to bottom, so I delete it.
<?php
$height = 50;
echo $height;
function hey($x)
{
echo " + $x";
return $x;
}
$i = 4;
while($i != 0)
{
$height += hey(2);
$i--;
}
echo " = $height";
?>
This is online demo: http://phpfiddle.org/main/code/6h1-x5z
<?php
function getResult($height = 50, $increment = 2, $times = 4){
echo $height."+";
$total = 0;
for ($i = 0; $i < $times; $i++){
$total += $increment;
if ($i != ($times-1)){
echo $increment."+";
}
else{
echo $increment." = ".($height+$total);
}
}
}
//usage
getResult(50,2,4);
//The print out: 50+2+2+2+2 = 58
?>
Related
function test_loop($x_values,$x, $y)
{
$x = $x + 1;
if($x < 10)
{
$x_values[] = $x."#"; // insert item 2#,3#,4# into array
}
//loop again if y is not = 3;
$y = $y + 1;
if($y < 5)
{
test_loop($x_values, $x, $y);
}
return $x_values;
}
function abc(){
$bababa = test_loop([],1,0);
foreach($bababa as $fpackage_id)
{
echo $fpackage_id;
}
}
abc();
Output :
2#
How to make the output become :
2#,3#,4#
if($y < 5)
{
test_loop($x_values, $x, $y);
}
You're not doing anything with the return value of the recursive function call.
You need to add the array returned from test_loop() to the existing array:
$x_values += test_loop($x_values, $x, $y);
Your code now prints: 2#3#4#5#6#
If you want the output to be 2#, 3#, 4#, 5#, 6#, you can use the implode() function instead of a loop:
echo implode (', ', $bababa);
You'll want to utilize pass by reference:
https://www.php.net/manual/en/language.references.pass.php
I haven't looked too closely at your code, you may need to make more changes than this... but this might work:
function test_loop(&$x_values,$x, $y)
{
$x = $x + 1;
if($x < 10)
{
$x_values[] = $x."#"; // insert item 2#,3#,4# into array
}
//loop again if y is not = 3;
$y = $y + 1;
if($y < 5)
{
test_loop($x_values, $x, $y);
}
return $x_values;
}
function abc(){
$bababa = test_loop([],1,0);
foreach($bababa as $fpackage_id)
{
echo $fpackage_id;
}
}
abc();
You should use array_push() function to add an element in the array
Documentation
if($x < 10)
{
array_push($x_values,$x.'#');
}
i have this code
that count one string accorances i just cant explain my use but it really nice for me plese see here https://ideone.com/pvAIjW
code:
<?php
$te = 'abcdefghijklmnopqrstuvwxyz1234567891011121314151617181920';
$text=strlen($te);
if($text <=10)
{
$g == 2;
}
else
{
$x=$text;
$y=2;
$tempMod = (float)($x / $y);
$tempMod = ($tempMod - (int)$tempMod)*$y;
if($tempMod ==0)
{
$g = $x / $y;
}
else
{
$x = $x+1;
$g = $x / $y;
}
}
echo $g;
in my achievement i want it as a function so i just call
$output=function($string);
echo $output to get my result
big thanks in advance
That would be like this. Just change all the instance of $te (although you can still use $te if you want, I just changed it to avoid confusion) with the parameter name (which is $string), then instead of echoing $g, return it:
function functionName($string){
$text=strlen($string);
if($text <=10)
{
$g == 2;
}
else
{
$x=$text;
$y=2;
$tempMod = (float)($x / $y);
$tempMod = ($tempMod - (int)$tempMod)*$y;
if($tempMod ==0)
{
$g = $x / $y;
}
else
{
$x = $x+1;
$g = $x / $y;
}
}
return $g;
}
$te = 'abcdefghijklmnopqrstuvwxyz1234567891011121314151617181920';
$varName = functionName($te);
NOTE: Not to be rude, but this kind of problem can be easily searched, so I suggest you try reading documentations pertaining to PHP or find online tutorials, most of the time they cover using and creating functions
<?php
$te = 'abcdefghijklmnopqrstuvwxyz1234567891011121314151617181920';
function name($value){
$text=strlen($value);
if($text <=10){
$value == 2;
}
else{
$x=$text;
$y=2;
$tempMod = (float)($x / $y);
$tempMod = ($tempMod - (int)$tempMod)*$y;
if($tempMod ==0){
$g = $x / $y;
}else{
$x = $x+1;
$value = $x / $y;
}
}
return $value;
}
echo name($te);
function functionName($string){
if(strlen($string) <=10) $g == 2;
else {
$x=strlen($string);
$y=2;
if(!(((float)($x / $y) - (int)((float)($x / $y)))*$y ==0)) $x++;
$g = $x / $y;
}
return $g;
}
$te = 'abcdefghijklmnopqrstuvwxyz1234567891011121314151617181920';
$varName = functionName($te);
var_dump($varName);
The code perhaps will be better shorter. Up to you.
Only you have to do is to send the $te variable like a parameter to the function:
function fnct($te){
$text=strlen($te);
if($text <=10)
{
$g == 2;
}else{
$x=$text;
$y=2;
$tempMod = (float)($x / $y);
$tempMod = ($tempMod - (int)$tempMod)*$y;
if($tempMod ==0)
{
$g = $x / $y;
}else{
$x = $x+1;
$g = $x / $y;
}
}
return $g;
}
And when you want to call this function you will use this code:
$te = 'abcdefghijklmnopqrstuvwxyz1234567891011121314151617181920';
$output = fnct($te);
echo $output;
I wanted to convert my Javascript code for creating a triangle to PHP codes, the Javascript codes works but the PHP code doesn't. This is what I have in my PHP codes, I tried to run it but ended up with a fatal error and undefined variable. I understand javascript but not php...
<?php
{
$size = $_POST['size'];
$firstChoice = $_POST['firstChoice'];
$secondChoice = $_POST['secondChoice'];
echo "<textarea>";
$allLines = '';
for ( $i = 1; $i <= $size; $i++ )
{
$oneLine = createLine ( $i, $i % 2 ? $FirstChoice : $secondChoice );
$allLines += $oneLine + "\n";
}
echo "$allLines";
function createLine ($size, $symbol) {
$aLine = '';
for ( $j = 1; $j <= $size; $j++ )
{
echo $aLine += $symbol;
}
echo "$aLine";
echo "</textarea>";
}
?>
It should look like this if size = 5, firstChoice = # and secondChoice = &
#
&&
###
&&&&
#####
What is $createLine ? Looks as if you're trying to use it as a function, but it is not defined anywhere.
Edit:
You need to declare the function in php
function createLine($size, $symbol) {
// code
}
And when you call it, just call it by the name, don't add a $.
$line = createLine($a, $b);
See documentation on php User-defined functions.
Working:
There were a few issues including: string concatenation should be using the . operator not +, a typo in $FirstChoice, and the function needs to be defined before you use it.
<?php
$size = $_POST['size'];
$firstChoice = $_POST['firstChoice'];
$secondChoice = $_POST['secondChoice'];
function createLine($size, $symbol) {
$aLine = '';
for ($j = 1; $j <= $size; $j++) {
$aLine .= $symbol;
}
return $aLine;
}
echo "<textarea>";
$allLines = '';
for ($i = 1; $i <= $size; $i++) {
$oneLine = createLine($i, $i % 2 ? $firstChoice : $secondChoice);
$allLines .= $oneLine . "\n";
}
echo "$allLines";
echo "</textarea>";
?>
Use createLine(...) and not $createLine(...)
I suppose you have javascript function like below
<script>
function createLine (...)
{
...
}
</script>
Write a class called math. It is to have one property called num. It also has one method (function) called factorial. This method is to start at 1 and multiply all of the integers to num. If num is 5 then you would multiply 1*2*3*4*5. Of course you are to do this in a loop.
Which loop should I use? For or do while? Also, do I need an inner loop?
I started with
For (i = 1; i <= 5; i++)
{
}
however, i'm stuck on what to do next...any suggestions?
You can do it using any loop. for loop can be converted to while and do .. while and opposite is true too.
for(i=0;i<5;i++)
is same as
i=0; while(i<5){i++;}
To to find the factorial you should multiply all the values from 1 to the number you want factorial of. So if $num = 5. Only one single loop is needed. You'd want to run this loop.
for($i=1;$i<$num;$i++){
$num*=$i;
}
I am not giving a full solution here because the question seems homework. If I give you full solution it will be spoon-feeding.
$result = 1;
$target = 5;
for ($i = 1;$ i <= $target; $i++)
{
$result *= $i;
}
echo $result;
or
$result = 1;
$target = 5;
while($target > 0) {
$result *= $target;
$target--; // You could do this all in one line, but for learners, this is clearer.
}
echo $result;
Each iteration you will want to multiply the total of the factorial by the value of $i
class Math {
public static function Factorial($factorial) {
$output = 1;
for($i = 2; $i <= $factorial; $i++)
$output *= $i;
return $output;
}
}
I've gotten to where I prefer the while(i--) loop:
<?php
class Math {
public $num = 0;
public function factorial() {
$result = 1;
$num = $this->num;
while ($num) {
$result *= $num--;
}
return $result;
}
}
$factor = new Math();
$factor->num = 5;
echo $factor->factorial();
?>
http://codepad.org/hUOgAoz2
I tried and tried and tried to get this code to work and kept coming up with zilch. So I decided to try it using "for loops" instead and it worked first try. Could somebody tell me why this code is no good?
<?php
$x = $y = 10;
while ($x < 100) {
while ($y < 100) {
$num = $x * $y;
$numstr = strval($num);
if ($numstr == strrev($numstr)) {
$pals[] = $numstr;
}
$y++;
}
$x++;
}
?>
you should reset y=10 inside the first while.
$x = 10;
while ($x < 100) {
$y = 10;
while ($y < 100) {
$num = $x * $y;
$numstr = strval($num);
if ($numstr == strrev($numstr)) {
$pals[] = $numstr;
}
$y++;
}
$x++;
}
You need to reset y before the y loop begins.
While($x < 100){
$y=10; //... rest of code
For loops which loop over an integer that is incremented I would prefer the for-loop:
for ($x=0; $x < 100; $x++) {
for ($y=10; $y<100; $y++) {
$num = $x * $y;
$numstr = strval($num);
if ($numstr == strrev($numstr)) {
$pals[] = $numstr;
}
}
}
IMHO this is much more readable and it's shorter, too.