Solved

complicated PHP manual parts

Posted on 2008-06-15
10
224 Views
Last Modified: 2013-12-13
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
Comment
Question by:locke2005
  • 6
  • 3
10 Comments
 
LVL 142

Accepted Solution

by:
Guy Hengel [angelIII / a3] earned 80 total points
Comment Utility
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
 
LVL 2

Assisted Solution

by:Airyck666
Airyck666 earned 320 total points
Comment Utility
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
 

Author Comment

by:locke2005
Comment Utility
Please, explain the last 3 complicated PHP manual parts!
0
 
LVL 2

Assisted Solution

by:Airyck666
Airyck666 earned 320 total points
Comment Utility
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
 
LVL 2

Assisted Solution

by:Airyck666
Airyck666 earned 320 total points
Comment Utility
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
Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 2

Assisted Solution

by:Airyck666
Airyck666 earned 320 total points
Comment Utility
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
 
LVL 2

Assisted Solution

by:Airyck666
Airyck666 earned 320 total points
Comment Utility
oops... in my code above, there's a typo
$a = -1;

was supposed to read (in both functions)

$someVar = -1;
0
 

Author Comment

by:locke2005
Comment Utility
There is only the #2 remaining! Thank you for the help so far!
0
 
LVL 2

Expert Comment

by:Airyck666
Comment Utility
#2?  The ternary one?  Just answered.
0
 

Author Closing Comment

by:locke2005
Comment Utility
Thanks a lot!!!
0

Featured Post

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

Suggested Solutions

Part of the Global Positioning System A geocode (https://developers.google.com/maps/documentation/geocoding/) is the major subset of a GPS coordinate (http://en.wikipedia.org/wiki/Global_Positioning_System), the other parts being the altitude and t…
These days socially coordinated efforts have turned into a critical requirement for enterprises.
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to count occurrences of each item in an array.

771 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now