- With Modulator
- <?php
- $num = 14;
- if($num%2==0) {
- echo 'The number is even.';
- } else {
- echo 'The number is odd.';
- }
- ?>
2. With Bitwise Operator
- <?php
- $num = 14;
- if($num&1) {
- echo 'The number is odd.';
- } else {
- echo 'The number is even.';
- }
- ?>
- <?php
- $num = 14;
- if($num%2==0) {
- echo 'The number is even.';
- } else {
- echo 'The number is odd.';
- }
- ?>
2. With Bitwise Operator
- <?php
- $num = 14;
- if($num&1) {
- echo 'The number is odd.';
- } else {
- echo 'The number is even.';
- }
- ?>
Today I was amazed with a question asked by one of my IT friends.
What’ll be the output of setTimeout function if I declare it inside the for loop like this.
- for(i=0;i<3;i++)
- {
- console.log(i);
- }
-
My answer was
- 0
- 1
- 2
-
But the answer was not correct as he challenged and I checked it too. The answer was ‘2’ only.
I tried to know why the output is so. I thought that this is because of threading it’ll be skipping the setTimeout function some time due to thread concept. But when I changed the condition to i<10000, but the output was same. Finally I’s not able to know the situation and problem.
At last I searched on forems, and I got my answer which is as follows:
The function argument to setTimeout
is closing over the loop variable. The loop finishes before the first timeout and displays the current value of i
, which is 4
.
Because JavaScript variables only have function scope, the solution is to pass the loop variable to a function that sets the timeout. You can declare and call such a function like this:
- for (var i = 0; i < 3; i++) {
- (function (x) {
- setTimeout(function () { alert(x); }, 100);
- })(i);
- }
You might also check this Q/A on stackoverflow
CODE:
-
- <?php
- //get values to swap
- for($b=0;$b<10;$b++)
- {
- }
- echo "<div style='float: left;margin-right: 20px;'>Before Sorting<br/>";
- for($b=0;$b<sizeof($a);$b++)
- {
- echo $a[$b]."<br/>";
- }
- echo "</div>";
- //swap started
- for($i=0;$i<sizeof($a);$i++)
- {
- for($j=0;$j<sizeof($a);$j++)
- {
- $swap="";
- {
- $swap=$a[$j];
- $a[$j]=$a[$j+1];
- $a[$j+1]=$swap;
- }
- }
- }
- echo "<div style='float: left;margin-right: 20px;'>After Sorting<br/>";
- for($b=0;$b<sizeof($a);$b++)
- {
- echo $a[$b]."<br/>";
- }
- echo "</div>";
- echo "<div style='float: left;margin-right: 20px;'>In Descending Order<br/>";
- {
- echo $a[$b]."<br/>";
- }
- echo "</div>";
- ?>
-
OUTPUT:
Hey guys, girls,
Here I’m going to teach you too simple lightbox popup window to see it before.
Add these lines into your body tag whenever you want. If may be you can arrange it according to your requirement. But make sure to be having position:fixed; and z-index:100 or 100+;
And then add these JavaScript code to yor head tag
- function hidePopUp(){
- $('#divPopUp').html('');
- $('#divPopUp').hide();
- }
- $(document).ready(function(){
- $('#divPopUp').hide();
- $('.small_image').click(function(){
- $('#divPopUp').html('</p><div align="center">---------------Here Add Your Tag/Content whatever you want---------------<br/><span onclick="hidePopUp()" style="cursor: pointer">CLOSE</span></div>');
- $('#divPopUp').show();
- });
- });
Here is a complete example of code which I have used it for image gallery section:
- <html>
- <head>
- <script>
- function hidePopUp(){
- $('#divPopUp').html('');
- $('#divPopUp').hide();
- }
- $(document).ready(function(){
- $('#divPopUp').hide();
- $('.small_image').click(function(){
- var largeImageSrc=$(this).attr('src');
- largeImageSrc=largeImageSrc.replace('small','big');
- $('#divPopUp').html('<div align="center"><img src="'+largeImageSrc+'"/><br/><span onclick="hidePopUp()" id="btnClose" style="cursor: pointer">CLOSE</span></div>');
- $('#divPopUp').show();
- });
- });
- </script>
- </head>
- <body>
- <?php
- echo '<ul class="gallerylist">';
- {
- echo '<li><div align="center" style="">'.$row['image_name'].'</div><img class="small_image" style="cursor:pointer;" src="admin/gallery_image/small/'.$row['image_id'].'.'.$row['image_ext'].'"/></li>';
- }
- echo '</ul>';
- ?>
- <div id="divPopUp" style="border: solid 3px #408080;position: fixed;top: 5%;left: 20%;width: 60%;height: 70%;background: #002200;color: #d9e3e8;z-index: 101;"></div>
- </body>
- </html>
See the Pen Light Box by AHMAD ASJAD (@ahmadasjad) on CodePen.0
Happy coding!!!!!!!