?
Solved

complicated PHP manual parts

Posted on 2008-06-15
10
Medium Priority
?
237 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 6
  • 3
10 Comments
 
LVL 143

Accepted Solution

by:
Guy Hengel [angelIII / a3] earned 320 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 1280 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
7 Extremely Useful Linux Commands for Beginners

Just getting started with Linux? Here's a quick start guide that has 7 commands that we believe will come in handy.

 
LVL 2

Assisted Solution

by:Airyck666
Airyck666 earned 1280 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 1280 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 1280 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 1280 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

Why Off-Site Backups Are The Only Way To Go

You are probably backing up your data—but how and where? Ransomware is on the rise and there are variants that specifically target backups. Read on to discover why off-site is the way to go.

Question has a verified solution.

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

Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
3 proven steps to speed up Magento powered sites. The article focus is on optimizing time to first byte (TTFB), full page caching and configuring server for optimal performance.
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
Suggested Courses

762 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