I am getting output:
"Your number is 49
Your number is 50
Not possible"
But why?What is the if-else statement say in here. How the pre and post increment works here?
<?php
$num = 49;
if($num % 2)
{
echo "Your number is ";
echo($num);
}
if($num++ % 3)
{
echo "Your number is ";
echo($num);
}
if(++$num % 3)
{
echo "Your number is ";
echo($num);
}
else
echo "Not possible";
In php boolean expression returns 1 for true and 0 for false
So in first if 49 % 2 return 1 so that block will execute
Second if also execute like first one and then num incremented and becomes 50
In 3rd if first num incremented then calculate 51 % 3 which returns 0 so the else block executes
<?php
$num = 49; // num is 49
if($num % 2) // 49%2 = 1 so basically true
{
echo "Your number is ";
echo($num); // print 49
}
if($num++ % 3) // (49++ = 50 % 3 = 2) post increment basically u r setting $num = $num+1
{
echo "Your number is ";
echo($num); //print 50
}
if(++$num % 3) // pre increment (51%3 = 0) basically false
{
echo "Your number is ";
echo($num);
}
else
echo "Not possible"; // so it show this
Related
I wanted to echo an image every after 3 post via XML here is my code :
<?php
// URL of the XML feed.
$feed = 'test.xml';
// How many items do we want to display?
//$display = 3;
// Check our XML file exists
if(!file_exists($feed)) {
die('The XML file could not be found!');
}
// First, open the XML file.
$xml = simplexml_load_file($feed);
// Set the counter for counting how many items we've displayed.
$counter = 0;
// Start the loop to display each item.
foreach($xml->post as $post) {
echo '
<div style="float:left; width: 180px; margin-top:20px; margin-bottom:10px;">
image file</a> <div class="design-sample-txt">'. $post->author.'</div></div>
';
// Increase the counter by one.
$counter++;
// Check to display all the items we want to.
if($counter >= 3) {
echo 'image file';
}
//if($counter == $display) {
// Yes. End the loop.
// break;
//}
// No. Continue.
}
?>
here is a sample first 3 are correct but now it doesn't loop idgc.ca/web-design-samples-testing.php
The easiest way is to use the modulus division operator.
if ($counter % 3 == 0) {
echo 'image file';
}
How this works:
Modulus division returns the remainder. The remainder is always equal to 0 when you are at an even multiple.
There is one catch: 0 % 3 is equal to 0. This could result in unexpected results if your counter starts at 0.
Going off of #Powerlord's answer,
"There is one catch: 0 % 3 is equal to 0. This could result in
unexpected results if your counter starts at 0."
You can still start your counter at 0 (arrays, querys), but offset it
if (($counter + 1) % 3 == 0) {
echo 'image file';
}
Use the modulo arithmetic operation found here in the PHP manual.
e.g.
$x = 3;
for($i=0; $i<10; $i++)
{
if($i % $x == 0)
{
// display image
}
}
For a more detailed understanding of modulus calculations, click here.
every 3 posts?
if($counter % 3 == 0){
echo IMAGE;
}
You can also do it without modulus. Just reset your counter when it matches.
if($counter == 2) { // matches every 3 iterations
echo 'image-file';
$counter = 0;
}
How about: if(($counter % $display) == 0)
I am using this a status update to show a "+" character every 1000 iterations, and it seems to be working good.
if ($ucounter % 1000 == 0) { echo '+'; }
It will not work for first position so better solution is :
if ($counter != 0 && $counter % 3 == 0) {
echo 'image file';
}
Check it by yourself. I have tested it for adding class for every 4th element.
I have a conditional statement to check if a number is odd or even in PHP
<?php
$stdin = fopen("php://stdin", "r");
fscanf($stdin, "%d\n", $N);
fclose($stdin);
function weird_or_not($N) {
//check that n is between 1 and 100
if ($N >= 1 && $N <= 100) {
//check if n is even
if ($N % 2 == 0) {
// check if n is in the inclusive range 2 - 5
if ($N >= 2 && $N <= 5) {
echo 'Not Weird';
}
// check if n is in the inclusive range 6 - 20
elseif ($N >= 6 && $N <= 20) {
echo 'Weird';
}
// check if n is greater than 20
elseif ($N > 20) {
echo 'Not Weird';
}
}
// otherwise n is odd
else {
echo 'Weird';
echo $N % 2;
}
}
}
echo weird_or_not($stdin);
?>
When I have an input of an even number it prints out 'Weird', and when I print out $N % 2, for any even number it gives the result as 1 when it should be 0. So why is an even number modulo 2 returning 1 when it should return 0?
I'm trying to make a pagination where I can limit how many numbers it will show in the navigation so it will be something like this
< 1 ... [3] 4 5 ... 9 >
The script I have now is working fine until I'm at the last couple of pages. Lets say I set max numbers allowed in the navigation to 4 and number of pages to 9. Then it will work to page 7, but from page 7 it will start shrink in numbers and not put them in front of the current page number. It will behave like this
< 1 ... [7] 8 9
< 1 ... [8] 9
When I'm at the last page the navigation will only show
< 1 ... 9
What i would like it to look like is like this
< 1 ... 6 [7] 8 9 >
< 1 ... 6 7 [8] 9 >
< 1 ... 6 7 8 [9]
I have tried to make this work for some hours now but, are kind of stuck. Any suggestions?
// Method to output the pagination
function output_pagination($max_per_page, $table) {
$page_number = $this->valid_page_number($this->get_page_number(), $table); // Get current page number
$num_pages = $this->count_table_rows($table) / $max_per_page; // Set the number of pages
$max_nav_pages = 4; // Max numbers of pages in the page navigation
$start = $page_number-1; // Value to start the loop on
// Set start to 1 if it equals 0
if($start === 0) {
$start = 1;
}
// Keep loop going until $i no longer is greater or equal to number of pages
for($i = $start; $i <= $num_pages; $i++) {
// Output prev arrow + '...' + 1 if its the start of the pagination and page number not equals 1 ($i = 1 and page number is > $i)
if(($i === $start) && ($page_number > 1)) {
// Check if number of pages are greater then max allowed in navigation and if page number not equals 2
if(($num_pages > $max_nav_pages) && ($page_number !== 2)) {
echo "<< ";
echo "< ";
echo "1 ";
echo " ... ";
$i = $page_number - 1;
} else {
echo "<< ";
echo "< ";
echo $i;
}
}
// Output current page in bold text if $i equals or are greater then $start and page number equals $i
else if (($i >= $start) && ($page_number === $i)) {
echo " <b>[".$i."]</b> ";
}
// Output "...", last page number and next page arrow. if we have 10 pages or more. Then break the loop. We are done.
else if($i === $start + $max_nav_pages) {
// Check if page number equals number of pages - max allowed nav pages + 1
if($page_number === $num_pages-$max_nav_pages+1) {
echo $i;
echo " >";
echo " >>";
break;
} else {
echo " ... ";
echo $num_pages;
echo " >";
echo " >>";
break;
}
}
// Output next page number if $i is greater then $start and $i + 1 is equal or greater then number of total pages
else if(($i > $start) && ($i + 1 <= $num_pages)) {
echo " ".$i." ";
}
else { // Output last page number and next page arrow. We are done.
echo $i;
echo " >";
echo " >>";
}
}
}
Let's say you are in page 8, then the loop always starts from $page_number - 1 which will be 7, which means 6 will never be displayed. You need to start your loop from $page_number - $max_nav_pages and determine which numbers should be displayed that way.
I have a problem where I need random values in php. I got all that working. But when I need, let's say, 10 random values, I need 8 of them to be at least a certain integer. How can I achieve this?
Kind regards,
Leon
<?php
$vertices = $_POST["vertices"];
echo "Vertices: $vertices <br>";
$edges = $_POST["edges"];
echo "Edges: $edges <br>";
$length = $_POST["length"];
echo "Length: $length <br><br>";
$temp1 = $edges;
for($x = 0; $x < $vertices; $x++){
for($i = 0; $i < $x; $i++){
echo "_ ";
}
for($y = $x; $y < $vertices; $y++){
$value = rand(0,1);
if($x == $y){
echo "0 ";
} else {
if($value == 1 && $temp1 > 0){
echo "1 ";
$temp1--;
} else {
echo "0 ";
}
}
}
echo "<br>";
}
?>
<html>
</font>
</html>
This will output something like this:
Vertices: 5
Edges: 8
Length: 4
0 0 1 0 1
_ 0 0 1 0
_ _ 0 1 0
_ _ _ 0 1
_ _ _ _ 0
However, I need to have exactly 8 1's on the upper half of the matrix. I don't know how to achieve it, because now it will do a random number under 8.
I would take another approach:
There are $vertices * ($vertices - 1) / 2 positions you need to fill (10).
There should be $edges times 1's and the rest 0's (8 and 2).
You can fill an array with 8 1's and 2 0's.
Shuffle the array.
Loop over your matrix and pop an element of the array for every position you need to fill.
I've putted a script together a bit with array's and stuff.
Now in the end, it checks for a score with checkboxes and divides it by ten.
Then with number format i round it off two 2 decimals, it looks like this.
$number = $score / $total;
$number = $number * 10;
$number = number_format(round($number,2),2,',','.');
echo "The number is: $number <br/>";
Then later on, i'm doing this.
if($number < 4 && $number > 0)
echo 'You're number is between zero and 4';
else if($number > 6 && $number < 4)
echo ' You're number is between 4and 6';
else if($number < 8 && $number > 6)
echo' You're number is between 6 and 8';
else if($number < 10 && $number > 8)
You're number is between 8 and 10';
Now, if the number is like, 0.50 or 1.50 or 2 or 5.50 it's shows the text i've provided.
However, if the number is like 4,13 or 7,85 or 9,13 it doesn't.
Searched now quite a while but can't figure it out.
Do you people see a solution?
Hope i was clear enough!
Thanks in advance.
Is this what you're looking for?
<?php
$score = 7; //Example values
$total = 32;
$number = $score / $total;
$number = $number * 10;
$number = round($number,2);
echo 'The number is: ',number_format($number,2,'.',','),' <br/>';
if($number < 4 && $number > 0)
echo 'Your number is between zero and 4';
else if($number < 6 && $number > 4)
echo 'Your number is between 4 and 6';
else if($number < 8 && $number > 6)
echo 'Your number is between 6 and 8';
else if($number < 10 && $number > 8)
echo 'Your number is between 8 and 10';
?>
The following statement will never validate true:
if($number > 6 && $number < 4)
Do you know any number that's higher than six and smaller than four?
The code also contains numerous syntax errors, for example:
echo 'You're number is between zero and 4';
The apostrophe needs to be escaped using a backslash:
echo 'You\'re number is between zero and 4';
Or use double quotes for strings containing apostrophes:
echo "You're number is between zero and 4";
Finally, grammar whise, you're should be your :)