Trying to solve this php code getting error - php

i am trying to print 2 4 6 8 10 with spaces, but it prints 246810.
for($i = 1; $i<= 5; $i++){
echo $i * 2.' ';
}
i am getting error : "This page isn’t working"

This might help.
for($i = 1; $i<= 5; $i++){
echo ($i * 2).' ';
}

try:
for ($i = 1; $i <= 5; $i++) {
echo ($i * 2) . ' ';
}
the main reason that your code fails is that the 2 is connected with a dot .
php interprets the 2 as an invalid type as it expects a digit next to it. e.i. 2.0
separating the 2 with a space or in the example with a parenthesis (to explicitly tell the logic) will make the code work

Problem:-2.-> php interprets the 2 as an invalid type because it is expecting a digit next to it something like 2.0. when you add space that simply tell it to apply multiplication first and then add space with it.
1.Either add a space before . like below:-
<?php
for($i = 1; $i<= 5; $i++){
echo $i * 2 .' ';
}
Output:-https://eval.in/856709
2.Or put multiplication into bracket like below:-
<?php
for($i = 1; $i<= 5; $i++){
echo ($i * 2).' ';
}
Output:-https://eval.in/856710

Try this,
for($i = 1; $i<= 5; $i++){
echo ($i * 2).' ';
}

Related

How I can make pattern Pyramid number php like this

How I can make pyramid number like this
1
212
32123
Code:
<?php
for($i=2; $i<=5; $i++){
for($x=$i; $x>=2; $x--){
echo "$x ";
}
echo "<br/>";
}
for($i=1; $i<=5; $i++){
for($x=1; $x<=$i; $x++){
echo "$x &nbsp";
}
echo "<br/>";
}
You seem to printing numbers in descending order, adding a new line and then go to print numbers in ascending order in another line. This makes it too late to do so since you already lost the current line.
Instead, for every row, go from row number till 1 and print numbers and repeat the same in the same line from 2 till row number and then add a new line like below:
<?php
function printPyramid($rows){
for($i = 1; $i <= $rows; ++$i){
echo str_repeat(" ", $rows - $i);
for($j = $i; $j >= 1; --$j){
echo $j;
}
for($j = 2; $j <= $i; ++$j){
echo $j;
}
echo PHP_EOL; // or echo "<br/>";
}
}
printPyramid(7);
Online Demo (view the output in HTML format by clicking on the eye icon)

how to fix php for loop output displaying 1?

Here is my php code given below when I run it in my browser its display 1 continuously. My question is why its show like this? After execution its displays a fatal error : maximum execution time is exceeded. what is that?
<?php
for ($i=1; $i<=5; $i+1) {
echo $i." ";
}
?>
Give me proper answer. Make me sure that what is the execution time of php code?
TIA
$i+1 does not increment the value of $i. It only adds 1 to what is in $i but it does not assign it back to $i. Your loop does this:
$i = 1
while ($i<=5) {
echo $i." ";
$i+1;
}
$i+1 on it's own doesn't do anything.
You need something like $i = $i + 1. Or for short $i += 1. Or even shorter and better: $i++.
for ($i=1; $i<=5; $i++) {
echo $i . " ";
}
It is because of $i+1 in for loop. This is basically an expression and it produces a result but you never assign this result to $i. Therefore you would rather do something like $i = $i + 1 or, in real life, use incrementation $i++. So the final code will looks like:
for ($i = 1; $i <= 5; $i++) {
echo $i." ";
}
use $i++ not $i+1, $i++ is $i=$i+1
Change your code to
<?php
for ($i=1; $i<=5; $i++) {
echo $i." ";
}
?>
because first you must set value for $i
You need to do
<?php
for( $i = 1; $i <= 5; $i++) {
echo $i." ";
}
?>
Now you just say 1 + 1 but you don't assign it to anything. You could use $i = $i + 1 but it's the same as $i++
<?php
for ($i=1; $i<=5; $i++) {
echo $i." ";
}
?>
in a for loop, every turn you need to increase value of $i. but you forgot to increase value of $i. You wrote $i +1 but it needs to be assigned with new value of $i.
In short, you should change $i +1 to $i = $i +1 or $i ++
the right code:
<?php
for ($i=1; $i<=5; $i = $i+1) {
echo $i." ";
}
?>
You are not changing the value of $i in the loop. Either $i =$i +1 or $i++ instead of $i + 1 will do.
You've problem in printing the line. It should look like
<?php
for ($i=1; $i<=5; $i++) {
echo $i;
}
?>

PHP increment string value each iteration

I want to increment the int on the end of my string which together makes up the complete value.
$btnid = 'btnid1';
for($i = 1; $i < $countP; $i++) {
$btnid = 'btnid' . ++;
}
I tried different types of concatenation but I can't seem to get it to work if I just set it to 1 it works but I need the string there too.
Just append $i to the string btnid in each loop iteration.
$string = 'btnid';
for($i = 1; $i < $countP; $i++) {
$btnid = $string . $i;
}
As a side note (and seriously, DO NOT actually do this) so long as you don't go past btnid9, you can just increment the string.
$btnid = 'btnid1';
for($i = 1; $i < 9; $i++) {
$btnid++;
}
echo $btnid; // btnid9
If you go over, things get a bit weird:
$btnid++;
echo $btnid; // btnie0
Manual page: http://php.net/manual/en/language.operators.increment.php

Adding or condition in for loop

How can I add or in for loop check?
for example:
for ($i=1; ($i <= $untilPoint) or ($i <= $points); $i++){
.. code ...
}
Exactly what you typed is valid PHP. For example, this program:
<?
$untilPoint = 3;
$points = 5;
for ($i=1; ($i <= $untilPoint) or ($i <= $points); $i++){
echo("$i\n");
}
?>
prints this:
1
2
3
4
5
(tested in PHP 5.2.17, run from the Bash prompt).
That said, I wonder if maybe you really need and rather than or? If $i is an index into an array of points, where $points is the number of points and $untilPoint is an arbitrary cutoff, then you want and, not or.
This is how it should be done, probably it runs slightly faster.
You can separate with , see http://php.net/manual/en/control-structures.for.php for more information
<?
$untilPoint = 3;
$points = 5;
for ($i=1; $i <= $untilPoint, $i <= $points; $i++){
echo($i , "\n");
}
?>

0's Not being Stripped from the front of a numeric value

I am working on a project for a friend's website which is suppose to generate completely random phone numbers to be displayed on a "fake" review board. I figured the best way to do with would be for me to to generate out each section separably. So 3-3-4, but no matter what I do, every time there is a 0 in front the code cuts it off. Here's an example of what I mean:
http://www.shiningashes.net/Test.php
yet this is what I have for the code:
<?php
for ($i = 0000; $i <= 9999; $i++) {
echo $i;
echo "<br>";
}
?>
How do I get the 0's to stop being cropped out so the 0's display? 0001, 0021, 0123, etc?
You can use str_pad
for ($i = 0; $i <= 9999; $i++) {
echo str_pad($i, 4, '0', STR_PAD_LEFT);
echo "<br>";
}
You can use printf to format your output:
<?php
for ($i = 0; $i <= 9999; $i++) {
printf("%04d<br>\n",$i);
}
?>
You need to make your variable a string if you want to keep the zeros. That would mean using quotes, and never using numeric operators on it. But since you depend on using ++ on it, I suggest the following hack:
<?php
for ($i = 10000; $i <= 19999; $i++) {
$str=substr ( $i , 0 , 4 );
echo $i;
echo $str;
echo "<br>";
}
?>
You will need to convert your integer to a string when printing it.
<?php
for ($i = 0; $i <= 9999; $i++) {
printf("%04d<br />", $i);
}
?>
Check the documentation for printf/sprintf for more information.
Kind regards,
Stefan

Categories