• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 246
  • Last Modified:

complicated PHP manual parts

What do these parts mean???


Note that the assignment copies the original variable to the new one (assignment by value),
so changes to one will not affect the other. This may also have relevance if you need to copy
something like a large array inside a tight loop. Assignment by reference is also supported,
using the $var = &$othervar; syntax. 'Assignment by reference' means that both variables end
up pointing at the same data, and nothing is copied anywhere. To learn more about references,
please read References explained. As of PHP 5, objects are assigned by reference unless
explicitly told otherwise with the new clone keyword.

Assignment Operators - Manual

http://www.php.net/manual/en/language.operators.assignment.php 


Note:  Please note that the ternary operator is a statement, and that it doesn't evaluate to a
variable, but to the result of a statement. This is important to know if you want to return a
variable by reference. The statement return $var == 42 ? $a : $b; in a return-by-reference
function will therefore not work and a warning is issued in later PHP versions.

Comparison Operators - Manual

http://www.php.net/manual/en/language.operators.comparison.php


// "||" has a greater precedence than "or"
$e = false || true; // $e will be assigned to (false || true) which is true
$f = false or true; // $f will be assigned to false
var_dump($e, $f);

// "&&" has a greater precedence than "and"
$g = true && false; // $g will be assigned to (true && false) which is false
$h = true and false; // $h will be assigned to true
var_dump($g, $h);

Logical Operators - Manual

http://www.php.net/manual/en/language.operators.logical.php


Note:  You should never use parentheses around your return variable when returning by
reference, as this will not work. You can only return variables by reference, not the result
of a statement. If you use return ($a); then you're not returning a variable, but the result
of the expression ($a)  (which is, of course, the value of $a).

return - Manual

http://www.php.net/manual/en/function.return.php


If the target server interprets the target file as PHP code, variables may be passed to the
included file using a URL request string as used with HTTP GET. This is not strictly speaking
the same thing as including the file and having it inherit the parent file's variable scope;
the script is actually being run on the remote server and the result is then being included
into the local script.

include - Manual

http://www.php.net/manual/en/function.include.php
0
locke2005
Asked:
locke2005
  • 6
  • 3
6 Solutions
 
Guy Hengel [angelIII / a3]Billing EngineerCommented:
question: did you already work with variables by reference, or with some language like C where the notion of pointers is very strong.

in short: when you have a variable, you have actually 2 parts:
* the value if the variable (which is in a "box" = memory location)
* and the pointer, which links the variable to the memory box.

now, with some code:
$a = "alpha";  // define variable $a, and assign it (point to) the value "alpha"
$b = $a;   // define the variable $b, and COPY the value of $a to another box in memory, and let $b point to that memory box.
$a = "betha";  // set a new value to the variable $a. important: $b now still has a value of "alpha";
$b = &$a;  // this will not copy the value, but the pointer: $b will point to the same box in memory as $a does.
$a = "caesar";  // set a new value to $a. important: differently that with the above, $b points to the same place, hence will alse return "caesar" now.








0
 
Airyck666Commented:
As for #3
The difference is in the precedence in the operators. The order of precedence is
1: ||
2: =
3: Or

And for the "and"ing
1: &&
2: =
3: And

So using parenthesis to help illustrate the order, you could think of it like this:
$e = (false || true); // $e will be assigned to (false || true) which is true
($f = false) or true; // $f will be assigned to false
 
$g = (true && false); // $g will be assigned to (true && false) which is false
($h = true) and false; // $h will be assigned to true

Open in new window

0
 
locke2005Author Commented:
Please, explain the last 3 complicated PHP manual parts!
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
Airyck666Commented:
As for #4

If you are writting a function that is to return by reference, as soon as you put parenthesis around your return variable, it will evaluate the statement and return the result and not the reference.

Here's a very detailed article on references in php
http://www.derickrethans.nl/php_references_article.php
function &someFunction() {
    return $someVar;
}
// $a will now be a reference to $someVar
$a =& someFunction();
 
 
function &someFunction() {
    return ($someVar);
}
// $a will now have the value of $someVar and not reference
$a =& someFunction();

Open in new window

0
 
Airyck666Commented:
As for the last one, it basically means that if you include a php file using the full url, it be processed as php BEFORE it is included (if the server of the requested file is set to process php), so it will not inherit variables from the including page.  However you can pass variables on the query string.

Hopefully this makes sense... I haven't had my morning coffee yet
<?php
 
$myVar = 100;
 
// this WILL inherit $myVar
include 'somefile.php';
 
 
// this will include the somefile.php but it will NOT inherit $myVar
include 'http://www.myserver.com/somefile.php';
 
// However you can do this to provide the script with the $myVar variable
include 'http://www.myserver.com/somefile.php?myVar=$myVar';

Open in new window

0
 
Airyck666Commented:
Oh... missed one.. .#2

This is basically the same as #4.  If you have a return by reference function, using a terary statement on the return line will evaluate and return a value, not the reference.
function &someFunction() {
    $a = 1;
    $a = -1;
    $someVar = $someVar>0 ? $someVar : $a;
    // this will return the reference of $someVar
    return $someVar;
}
 
function &someFunction() {
    $a = 1;
    $a = -1;
    // this will return the value 1
    return $someVar > 0 ? $someVar : $a;
}

Open in new window

0
 
Airyck666Commented:
oops... in my code above, there's a typo
$a = -1;

was supposed to read (in both functions)

$someVar = -1;
0
 
locke2005Author Commented:
There is only the #2 remaining! Thank you for the help so far!
0
 
Airyck666Commented:
#2?  The ternary one?  Just answered.
0
 
locke2005Author Commented:
Thanks a lot!!!
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 6
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now