This my coding I'm not getting my answer via Infunction Value '$z'. Please Help
<form action="functions.php" method="post">
<input type="text" name="math">
</form>
<?php
$x = $_POST['math'];
function table($x, $y) {
$z = $x * $y;
return $z;
}
for ($y = 1; $y <=10; $y++) {
echo $x ."*". $y ."=". "<br>";
}
You have to call your function to get the output like below:-
for ($y = 1; $y <=10; $y++) {
$output = table($x, $y);
echo $x ."*". $y ."=". $output . "<br>";
}
A sample output:- https://eval.in/872649
You need to wrap php code inside isset() like below:-
<?php
if(isset($_POST['math'])){
$x = $_POST['math'];
function table($x, $y) {
$z = $x * $y;
return $z;
}
for ($y = 1; $y <=10; $y++) {
$output = table($x, $y);
echo $x ."*". $y ."=". $output . "<br>";
}
}
You need to change your loop as below.. You don't have call the function table anywhere in your code. call it in loop
for ($y = 1; $y <=10; $y++) {
$z = table($x, $y);
echo $x ."*". $y ."=".$z ."<br>";
}
Related
I am trying to write a code that is capturing number 3. using while loop as the condition, so that the for statement will be the evaluation condition of $x, of the while loop statement, and by the same time, using if statement to evaluate the value of $x=3 and so it can echo 'three..'; . please enlighten me. thank you
<?php
$x = 0;
$y = 5;
while ($x <= $y) {
for ($z = 0; $z < 3; $z++) {
if ($x = 3) {
echo 'three..' . "\n";
}
}
$y++;
}
In while loop, you should increment x variable.
If you increment y variable, it will always "true". So it will be endless loop.
<?php
$x = 0;
$y = 5;
while ($x <= $y) {
// code
$x++;
}
<?php
$x = 0;
$y = 5;
while ($x <= $y) {
for ($z = 0; $z < 3; $z++) {
echo $z;
}
if ($x == 3) {
echo 'three..' . "\n";
}
$x++;
}
function test_loop($x_values,$x, $y)
{
$x = $x + 1;
if($x < 4)
{
//I want to add $x value into $x_values variable, eg : $x_values = $x_values . $x;
//but $x_values = $x_values . $x; is not working, so I force to use $x_values = test_loop($x_values . $x . "##", $x, $y);
$x_values = test_loop($x_values . $x . "##", $x, $y);
}
//loop again if y is not = 3;
$y = $y + 1;
if($y < 3)
{
echo "kkk" . $y . "<br/>";
$x_values = test_loop($x_values . $x . "##", $x, $y);
}else{
echo "---------------------<br/>";
}
return $x_values;
}
function abc(){
$bababa = test_loop(0,1,0);
echo $bababa;
}
abc();
Output :
kkk1
kkk2
---------------------
kkk1
kkk2
---------------------
kkk1
kkk2
---------------------
kkk2
---------------------
02##3##4##5##3##4##2##3##4##3##
How to make the output become :
kkk1
kkk2
---------------------
02##3##
Why don't you try to make $x_values an array, and add values in $x_values as array items, $x_values[] = $x; and then when you're ready, just implode() those values into a string. Like this:
function test_loop($x_values,$x, $y)
{
$x = $x + 1;
if($x < 4)
{
$x_values[] = $x;
}
//loop again if y is not = 3;
$y = $y + 1;
if($y < 3)
{
echo "kkk" . $y . "<br/>";
$x_values[] = $x;
}else{
echo "---------------------<br/>";
}
return implode($x_values);
}
Just make sure that you also pass $x_values as an array initially:
function abc(){
$bababa = test_loop([0],1,0);
echo $bababa;
}
this is what i want.
123456
23456
3456
456
56
6
Hi, i have trouble with this loop.
<?php
for ($x = 7; $x >= 1; $x--) {
for ($y = 7; $y > $x; $y--) {
echo "  ";
}
$s = 7;
while ($s < $x) {
$f++;
$s--;
}
for ($f=1; $f < 7; $f++) {
echo "$f";
}
echo "<br>";
}
?>
this is what i got. I want to get the $f work but it is ignoring it.
You can make it simpler than you did.
for($x = 1; $x <= 6; $x++) {
for($y = 1; $y <=6; $y++){
if($x > $y)
echo "  ";
else
echo $y;
}
echo "<br>";
}
With x you control the lines and with y the columns. If the lines is greater than the column you print the spaces, and if not, the number.
So I have a list with variables (auto-generated), something like:
$won3 = 1;
$time3 = 4;
$won6 = 0;
$time6 = 5;
$won4 = 0;
$time4 = 5;
(...)
but with many more variables. Now I want to make a table with all the variables, so I used a for-loop, but $won1 has to be the first in the table, then $won2 etc...
But how can I recall this $won1 in a for-loop? I tried:
for ($X = 0, $X < $Y, $X++){
echo '$won'.$X;
}
but this does not do the job. Anyone knows how I can solve this?
Thanks in advance.
First... In for loops you can't use ,
for ($X = 0, $X < $Y, $X++){
Try this:
for ($X = 0; $X < $Y; $X++){
.
And you have to choices...
for ($X = 0; $X < $Y; $X++){
$var = 'won' . $X;
echo $$var;
echo ${'won' . $X};
}
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.