Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 6 years ago.
Improve this question
When using increment on variables in Javascript, lint says that x += 1 is preferred over x ++.
But what about in PHP?
Is there any difference between the += and ++ or does it just not really matter?
Well whatever you might say about conventions, try running the following...
$i = 1; $s = 's';
$i++; $s++;
echo $i.'<br>'.$s.'<br>';
$i = 1; $s = 's';
$i += 1; $s += 1;
echo $i.'<br>'.$s.'<br>';
the output is somewhat unexpected...
2
t
2
1
so I would say it could matter very much which is chosen!
x += 1 is rather equivalent to ++x.
All those expressions (x += 1, x++ and ++x) increment the value of num by one, but the value of x++ is the value x had before it got incremented.
Related
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 7 years ago.
Improve this question
This is my solution to Euler Project Problem 14
<?php
$count = 0 ;
$max = 0;
for($n = 2 ; $n < 1000000 ; $n++){
while ($n > 1)
{
if ($n % 2 == 0 )
{
$n = $n/2;
}
else
{
$n = 3*$n + 1 ;
}
$count += 1;
if($count > $max )
{
$max = $count;
$final = $n;
}
}
}
echo $final;
>?
It took so long to run.I looked some other solutions and they were very similar to my code logically,but they were running way too faster than mine.
My question is,what is it that makes my code inefficient? What am I missing here?
Thanks ^^
Your approach is straight forward and unpolished.
You can use dynamic programming in order to improve the runtime (complexity approach).
Let's say you want to compute for 5. You will have :
5 -> 16 -> 8 -> 4 -> 2 -> 1.
But if you do that you will also have computed the value for 8 and 16.
The idea is to store the value that you have already computed in order to save work when you will need them later.
Working on the complexity of an algorithm will be the key of some problems, so better get used to it early.
An other reason why it is slow, is the choice of the language. For example try with C it will run a lot faster.
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 7 years ago.
Improve this question
Can anyone explain in depth why this outputs 9
here is my code in PHP:
$x = 4;
$x = $x+++$x++;
echo $x;
Execution goes like this:
$x = $x++ + $x++; ($x = 4)
$x = 4 + $x++; ($x = 5)
$x = 4 + 5; ($x = 6)
$x = 9;
For a more detailed answer of a more complex example in Java, see this answer: Incrementor logic
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 8 years ago.
Improve this question
I have long decimal numbers that I'd like to make easier to read. There's many 0's involved and I would like to bold the end of the string from where the numbers stop being just 0's. The number will always have 8 decimal places, no more, no less. It's worth noting that I would like any trailing zeros to also be bold, I don't just want to avoid bolding 0 as a whole.
Example:
Before - 0.00004320
After - 0.00004320
Any help or pointers in the right direction much appreciated.
Thanks.
You can do it using a loop and some string manipulation:
function doTheThing($number) {
$length = strlen($number);
$period = strpos($number, '.') + 1;
for ($i = $period; $i < $length; $i++) {
if ($number[$i] !== '0') {
$number = substr($number, 0, $i).'<b>'.substr($number, $i).'</b>';
break;
}
}
return $number;
}
echo doTheThing('0.00004320');
Or you can do it using a regular expression replacement:
function doTheThing($number) {
return preg_replace('/^(\d+\.0*)(\d+)$/', '$1<b>$2</b>', $number);
}
echo doTheThing('0.00004320');
echo doTheThing(sprintf('%.8f', 0.00004320)); // if your number is not a string
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 need to divide an integer value into 3 parts using php
for eg: I have a value 9 the answer is like 3 + 3 + 3
if i have value 10 the ans like 3 + 3 + 4 or something like that
You can use a modulus function.
<?php
$yourInt=10;
$remainder=$yourInt % 3;
$third=floor($yourInt/3);
$lastBit=$third+$remainder;
echo "The numbers are $third + $third + $lastBit.";
?>
Output:
The numbers are 3 + 3 + 4.
If you have a value that is >= 3 you can do the following:
$number = 10;
$number1and2 = floor($number/3);
$number3 = $number - (2*$number1and2);
$num = 11;
$d = [$num/3,$num/3,$num/3];
$round = array_map('round', $d);
list($first, $second, $third) = $round;
$round = (is_float($num/3)) ? $num-(round($num/3)*3) : 0;
echo $first.' '.$second.' '.($third += (is_float($num/3)) ? $num-(round($num/3)*3) : 0);
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 have one question to you, and I hope you will help me. I want to write calculator but i don't have chance tu change operations , I can only use addition operation. I don't know what happened. can anyone help me?
look at it, what happened?
<?php
$x = isset($_POST['field']);
if($x == 1){
echo $x + 5;
}
elseif($x == 2){
echo $x - 5;
}
?>
You code is wrong as this line will evaluate only as 0 (false) or 1 (true) if condition is met:
$x = isset($_POST['field']);
You need to do it that way:
$x = isset($_POST['field']) ? $_POST['field'] : 0;
(where 0 is default value, $x is assigned to in case there's no $_POST['field]` set.
$x = isset($_POST['field']) ? (int)$_POST['field'] : 0;
You want the value, not the boolean if it's set or not.