Solved

complicated PHP manual parts

Posted on 2008-06-15
10
231 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 143

Accepted Solution

by:
Guy Hengel [angelIII / a3] earned 80 total points
ID: 21788682
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
ID: 21788703
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
ID: 21788742
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!

 
LVL 2

Assisted Solution

by:Airyck666
Airyck666 earned 320 total points
ID: 21788760
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
ID: 21788812
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
 
LVL 2

Assisted Solution

by:Airyck666
Airyck666 earned 320 total points
ID: 21788844
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
ID: 21788861
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
ID: 21788863
There is only the #2 remaining! Thank you for the help so far!
0
 
LVL 2

Expert Comment

by:Airyck666
ID: 21788878
#2?  The ternary one?  Just answered.
0
 

Author Closing Comment

by:locke2005
ID: 31467352
Thanks a lot!!!
0

Featured Post

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!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Things That Drive Us Nuts Have you noticed the use of the reCaptcha feature at EE and other web sites?  It wants you to read and retype something that looks like this.Insanity!  It's not EE's fault - that's just the way reCaptcha works.  But it is …
This article discusses how to create an extensible mechanism for linked drop downs.
The viewer will learn how to count occurrences of each item in an array.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

749 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