How to use 2 variables in For loop? [duplicate] - php

This question already has answers here:
Php for loop with 2 variables?
(7 answers)
Closed 6 years ago.
My PHP code (so far not working as I want it to) -
for ($x=0, $y=0; $x<=163, $y<=98 ; $x++, $y++) {
echo "Value of x = " . $x . " & y = " . $y . "<br>";
}
What I was trying to achieve is - it should echo even if one condition matches. Right now it stops when one condition is fulfilled, i.e. in this case when y=98, it stopped. Also there may be cases when y>=x in contrast to given code where x>y
Edit -
The duplicate marked question, did not solve my problem as in that question, range of both the variables were same, so it could have been achieved by the mentioned answer (by increasing one variable). In my case both variables has different range.
Also I tried this
for ($x=0, $y=0; $x<=163 || $y<=98 ; $x++, $y++) {
echo "Value of x = " . $x . " & y = " . $y . "<br>";
}
But it's also not helping me to achieve my desired output.
Edit 2 - I think I couldn't explain properly earlier, with what I wanted the output to be.
So I am trying to demonstrate by small e.g. with x=3, y=2
x=1, y=1
x=2, y=1
x=3, y=1
x=1, y=2
x=2, y=2
x=3, y=2
I am trying to achieve something like this. I don't know, what this ?matrix is called in maths (so it's possible my question title is wrong).

You can achieve that through a basic nested loop. Basically you loop over y in the outer loop and for each iteration of y you do a another for loop for x. Try this and adjust x and y as necessary.
<?php
for ($y=0; $y<=98 ; $y++) {
for ($x=0; $x<=163; $x++) {
echo "Value of x = " . $x . " & y = " . $y . "<br>";
}
}

Related

How Do I Count Up By Two In PHP? [duplicate]

This question already has answers here:
Adding 2 to a variable each time
(5 answers)
Closed 1 year ago.
So I'm trying to make it so the computer counts up to ten by two and making a new line each time. When I tested this my entire computer crashed. What's wrong with it?
<?php
$test = 0;
while ($test < 10) {
$test + 2;
echo $test . "/n";
}
?>
Change:
$test + 2;
to:
$test += 2;
Currently, $test is always 0 in your code, since you are not assigning a new value to it, hence the infinite loop.
Additionally, you would also want to change:
echo $test . "/n";
to:
echo $test . "\n";
Since you need to use a backslash to indicate a new line character.
N.B. In PHP you can also use the PHP_EOL constant to indicate the end of a line.
Here you just sum $test with 2 but did not assign it back to $test
$test + 2;
Trying replace it by $test += 2;
Another point is /n is not "making a new line" as you expected, change it to \n instead.
Although you can use while statement, this is a good case to use for instead.
for($i=0; $i < 10; $i += 2)
{
echo $i . PHP_EOL;
}
Usually while is used to evaluate something every loop, some variable that can change inside the while, like a bool variable or an "infinite" loop until break.
When you have a already defined number of loops, for is usually a better approach.

how can i add " to a variable in php?

so I'm trying to make a variable that contains a " sign
like if i had a variable called $x
i want my variable
$y = """ , $x , """
but i can't just write """ so what is the method of adding a " to a variable
im using php 5 but i don't think that's gonna help i mean im justwriting more because this site doesn't allow me to post but i can't explain more and i cant find my answer in another place
and again more writing yada yada , some questions are small so WHY just WHY does it think it's incomplete
You can do it like this
$x =11;
$y = '"""'.$x.'"""';
echo $y;
Result
"""11"""
now i finally figured out what i was doing wrong , i was using , instead of . between the values so the final code is
$y = '"' . $x . '"' ;

Echo statement not coming out as i want it [duplicate]

This question already has answers here:
Not getting the specific output without braces [closed]
(3 answers)
Closed 8 years ago.
This code only prints out '17' instead of "10 + 7 = 17". Why is this? And how can i solve this.
<?
$x = 10;
$y = 7;
echo $x . '+' . $y . '=' . $x+$y;
?>
What you should understand is that echo won't do anything until the result of the whole expression given to it is evaluated. This expression contains both . and + operators - the first one concatenates its operands (glues two strings together), the second adds numbers.
Now, you might think that + operator has a higher precedence than of . one - in other words, that result of $x + $y will be calculated before (eventually) glued to the rest of the string. But it's not. So we can say this statement is actually treated as...
echo ($x . '+' . $y . '=' . $x) + $y;
In other words, the result of all the string concatenation is added (converted to number) to $y, and only the result of this operation is printed.
The result of $x . '+' . $y . '=' . $x is '10+7=10', and it doesn't look like something that might be added. But guess what, PHP wants to be nice to you - and it assumes that you actually wanted to extract the first digits of a string when you tried to convert it to a number. So the whole line is treated as number 10. When added to 7, it's just 17 - that's why you got 17 echoed.
One possible workaround is getting rid of ., using , operator instead (as its precedence is lower - in fact, it's the lowest among operators):
echo $x, '+', $y, '=', $x + $y;
It could be simplified if one remember about such a handy feature of PHP as string interpolation:
echo "$x + $y = ", $x + $y;
<?php
$x = 10;
$y = 7;
function sum($a,$b){
return $a+$b;
}
echo $x.'+'.$y.'='.sum($x,$y);
?>

If-else statement in just one line for simple text [duplicate]

This question already has answers here:
if block inside echo statement?
(4 answers)
Closed 11 months ago.
I want to write some text Year: X | percentage: $percentage%, where X is based on the value contained in $year.
$year can be 2010, 2011... but also 0. In this case, we have to write the word "All"; otherwise, the year itself.
Year: 2011 | percentage: 2% // case $year not 0
Year: All | percentage: 2% // case $year == 0
This code seems very long for what is required:
echo "year: ";
if ($year==0)
echo "All";
else
echo $year;
echo " | Percentage: $percentage%";
So I wonder: how can we make this code shorter and clearer?
Note I posted my own answer because I wanted to share the way I found after spending some time working on it. Anyway, I see there are other ones that look quite good.
You can use a ternary operator:
echo ( condition ? "if condition true" : "otherwise")
For this specific case:
echo "Year: ". ((0==$year) ? "All" : $year) ." | Percentage: $percentage%";
Might not be a direct answer to you questions, but IMHO you should do something like:
$renderedYear = $year;
if ($year == 0) {
$renderedYear = 'All';
}
echo 'Year: ' . $renderedYear . ' | Percentage: ' . $percentage . '%';
Always prefer readability over shortness of code. Pixels on screen are waaaay cheaper than debugging time.
Also instead of concatenating you may want to use *printf.
I am sorry if this doesnt work (cant test it right now) but in C (which is somewhat similar to php) you can negate numbers directly, i belive you could also use:
echo "Year: ". (!$year ? "All" : $year) ." | Percentage: $percentage%";
I would style it using sprintf() as it's far easier to read.
echo sprintf("year: %s | Percentage: %s %%", ($year == 0) ? "All" : $year, $percentage);

combining arrays to get 3rd array value

I am doing a profile quiz. This is just an example of code. Ultimately I will do a loop for all possible combinations. Basically, if the student chooses answer 3 to question 1 (what is your height - small, medium, large, xl, xxl) I want their pgheight to be stored as 4, sgheight as 9, cheight as 4, etc... My final array will consist of 16 $pos and 16 $attr with 5 scores for each combination. Hope this makes sense. Thanks for any help.
e.g.
function height(){
$pos=array("pg","sg","c");
$attr=array("height","weight","strength");
$pgheight=array(1,2,3,4,5);
$sgheight=array(6,7,8,9,10);
$cheight=array(1,2,3,4,5);
$ans=3;
$i=0;
// THIS IS THE CODE THAT DOESN'T SEEM TO WORK.
// My logic is $pgheight=$pgheight[3];
$pos[$i].$attr[$i]=$pos[$i].$attr[$i]."[".$ans."]";
echo $pgheight;
}
you either want to eval the statement or use two dollar signs. you're reusing the variable pgheight so its a bit confusing. let me give you an example instead.
$fruit = array("apples", "oranges", "pears");
$drinks = array("beer", "cider", "wine");
$krislikes = "drinks";
$whichone = 0;
echo "Kris likes " . ${$krislikes}[$whichone] . "<br>";
// or
$answer = $$krislikes;
echo "Kris likes " . $answer[$whichone] . "<br>";
// or
eval('$the_answer=$' . $krislikes . '[' . $whichone . '];');
echo "Kris likes " . $the_answer . "<br>";
hope that helps.

Categories