## annas 3 years ago New tutorial on PHP contents: 1.Logical operators. 2.while loop 3.foreach loop

1. annas

LOGICAL OPERATORS: Logical operators are typically used with Boolean (logical) values; when they are, they return a Boolean value. However, the && and || operators actually return the value of one of the specified operands, so if these operators are used with non-Boolean values, they may return a non-Boolean value. TYPES OF LOGICAL OPERATORS: \$a and \$b And TRUE if both \$a and \$b are TRUE. \$a or \$b Or TRUE if either \$a or \$b is TRUE. \$a xor \$b Xor TRUE if either \$a or \$b is TRUE, but not both. ! \$a Not TRUE if \$a is not TRUE. \$a && \$b And TRUE if both \$a and \$b are TRUE. \$a || \$b Or TRUE if either \$a or \$b is TRUE.

2. annas

lets see examples: <?php // -------------------- // foo() will never get called as those operators are short-circuit \$a = (false && foo()); \$b = (true || foo()); \$c = (false and foo()); \$d = (true or foo()); // -------------------- // "||" has a greater precedence than "or" // The result of the expression (false || true) is assigned to \$e // Acts like: (\$e = (false || true)) \$e = false || true; // The constant false is assigned to \$f and then true is ignored // Acts like: ((\$f = false) or true) \$f = false or true; var_dump(\$e, \$f); // -------------------- // "&&" has a greater precedence than "and" // The result of the expression (true && false) is assigned to \$g // Acts like: (\$g = (true && false)) \$g = true && false; // The constant true is assigned to \$h and then false is ignored // Acts like: ((\$h = true) and false) \$h = true and false; var_dump(\$g, \$h); ?>

3. annas

WHILE LOOP: while loops are the simplest type of loop in PHP. They behave just like their C counterparts. The basic form of a while statement is: while (expr) statement The meaning of a while statement is simple. It tells PHP to execute the nested statement(s) repeatedly, as long as the while expression evaluates to TRUE. The value of the expression is checked each time at the beginning of the loop, so even if this value changes during the execution of the nested statement(s), execution will not stop until the end of the iteration (each time PHP runs the statements in the loop is one iteration). Sometimes, if the while expression evaluates to FALSE from the very beginning, the nested statement(s) won't even be run once. Like with the if statement, you can group multiple statements within the same while loop by surrounding a group of statements with curly braces, or by using the alternate syntax: while (expr): statement ... endwhile;

4. annas

Lets see an example: <?php /* example 1 */ \$i = 1; while (\$i <= 10) { echo \$i++; /* the printed value would be \$i before the increment (post-increment) */ } /* example 2 */ \$i = 1; while (\$i <= 10): echo \$i; \$i++; endwhile; ?> the working of while loop can be seen in the above example.

5. annas

FOREACH LOOP: The foreach construct provides an easy way to iterate over arrays. foreach works only on arrays and objects, and will issue an error when you try to use it on a variable with a different data type or an uninitialized variable. There are two syntaxes: 1.foreach (array_expression as \$value) statement 2.foreach (array_expression as \$key => \$value) statement The first form loops over the array given by array_expression. On each iteration, the value of the current element is assigned to \$value and the internal array pointer is advanced by one (so on the next iteration, you'll be looking at the next element). The second form will additionally assign the current element's key to the \$key variable on each iteration. Lets see an example:

6. annas

Example 1: <?php \$arr = array(1, 2, 3, 4); foreach (\$arr as &\$value) { \$value = \$value * 2; } // \$arr is now array(2, 4, 6, 8) unset(\$value); // break the reference with the last element ?> Example 2: <?php foreach (array(1, 2, 3, 4) as &\$value) { \$value = \$value * 2; } ?> NOTE: Reference of a \$value and the last array element remain even after the foreach loop. It is recommended to destroy it by unset().

7. annas

So thats it for this tutorial. Do comment on the tutorial.

8. annas

9. sami-21

good one !

10. annas

@Muskan , @mathslover , @MathSofiya @mathhasproblems

11. annas

thank you @sami-21

12. nilankshi

nice..

13. annas

@lalaly , @ash2326 , @moha_10 , @malihe

14. annas

@nilankshi thank you

15. Muskan

v nys....gr8 @annas

16. nilankshi

17. annas

@Muskan thanks

18. Hashir

great :D

19. annas

@Hashir thank you

20. Muskan

ur wlc

21. annas

@apoorvk , @Callisto

22. ParthKohli

Logical Operators https://developer.mozilla.org/en/JavaScript/Reference/Operators/Logical_Operators While loop: http://php.net/manual/en/control-structures.while.php Foreach loop: http://php.net/manual/en/control-structures.foreach.php Good luck doing it on your own next time.

23. mathslover

Wooh ! great tutorial annas. ... I think you are the master here of comp. science .. . thanks agan annas

24. ParthKohli

@mathslover Just look at the reply above yours.

25. mathslover

You mean that this is not personal tutorial ?

26. ParthKohli

No, it is not; and it hasn't cited the sources. Therefore, this can be called plagiarism. You have the evidence. "plagiarism (noun) The practice of taking someone else's work or ideas and passing them off as one's own." As defined by Dictionary.com

27. annas

my dear friend parth pointing finger at someone is quite easy... but practically doing something is different i can create a whole new website using php. between i took help from open source php manuals it not so called plagiarism ... so next time try to be more help than that . thank you for your feed back

28. annas

These are sentences taken to explain topics... i have a question the professor who taught us these subject they also take help from internet are they doing plagiarism too ??? i think not ... because things whose purpose is explain no matter what they are or where they are taken from comes under the category of helping .

29. mathslover

gr8 points @annas ..

30. annas

thanks @mathslover

31. annas

and one more thing i m just a source to bring stuff to help that has been developed many years ago

32. farmdawgnation

@annas I'm afraid Parth has a point. Although you wrote your examples on your own, you did take content from MDN and the PHP manual verbatim without citation, and that's plagiarism. I'm going to let Colm decide how he wants to handle this (either by deleting the question, etc, etc) - but you definitely should not do that again without providing links to the sources in YOUR posts.

33. annas

ok @farmdawgnation

34. annas

@cshalvey just want to say one thing php manuals are open source so if i took help to explain some concepts then whats wrong with it alot of people here do such stuff why i m being pointed ?

35. cshalvey

@Annas - Others *have* been warned about this kind of behavior. The issue here is not that you used the material - as you said, it's open-source so that's fine. The issue is *crediting* the source of this material. Otherwise, it does qualify as plagiarism as it appears you are passing off other people's work as your own. Please remember this in the future, as plagiarism is a very important issue to be aware of - ESPECIALLY now when every source of information is easily available on the internet. Thank you.

36. moha_10

wow awesome annas good job

37. annas

Ok @cshalvey i'll be careful next time.