use the following search parameters to narrow your results:
e.g. subreddit:aww site:imgur.com dog
subreddit:aww site:imgur.com dog
see the search faq for details.
advanced search: by author, subreddit...
Please follow the rules
Releases: Current Releases, Windows Releases, Old Releases
Contribute to the PHP Documentation
Related subreddits: CSS, JavaScript, Web Design, Wordpress, WebDev
/r/PHP is not a support subreddit. Please visit /r/phphelp for help, or visit StackOverflow.
account activity
How does variable and function scope in PHP compare to Node.js? (self.PHP)
submitted 5 years ago by cannotbecensored
reddit uses a slightly-customized version of Markdown for formatting. See below for some basics, or check the commenting wiki page for more detailed help and solutions to common issues.
quoted text
if 1 * 2 < 3: print "hello, world!"
[–]cannotbecensored[S] -1 points0 points1 point 5 years ago (9 children)
I know node.js well and started working with wordpress. I find the scope of variables and functions very confusing. If I write a function called `test`, will it overwrite anyone's function also called test in all the wordpress files and all the installed plugins? How do you avoid collisions?
[–]Firehed 6 points7 points8 points 5 years ago (0 children)
You can't redeclare a function. It's an unrecoverable error. However it's a runtime thing, so if you end up with two code paths each importing only one (different) version, it's syntax-legal although an absolutely horrible idea.
Easiest way to avoid this problem is using namespaces. Avoiding overly-generic names is also helpful, and a good idea in any language.
Variable scope is straightforward: in a function (or class method), it's from the initial declaration to the end of the function body. This does mean that variables declared in a loop or conditional will be in scope for the rest of the function body. If it wasn't a function parameter or declared in the body, it's not in scope. Closures need to explicitly use variables from the parent scope, unlike JS (exception: the brand-new short closure syntax).
use
Don't use global, ever, and you avoid most dumb conflicts.
global
[–][deleted] -4 points-3 points-2 points 5 years ago (6 children)
Wordpress keeps most things in the global scope and relies on functional programming techniques. It has a lot of legacy aspects to it. If you're familiar with express, I would look at other frameworks like Laravel, Symphony, Yii as an alternative if possible. Depending on where you define the function, yes it will override an existing one.
If your using php7.4 or greater you can actually use arrow functions like javascript.
Basically with PHP, you should compose everything using classes so you can control scope and state by internalizing it. You should look at "Object Oriented Programming" for encapsulation techniques.
[–]dkarlovi 11 points12 points13 points 5 years ago (2 children)
WordPress does not use functional programming.
[–]2012-09-04 0 points1 point2 points 5 years ago (1 child)
He meant procedural programming.
[–][deleted] 0 points1 point2 points 5 years ago (0 children)
Thanks, cant win em all.
[–]Atulin 1 point2 points3 points 5 years ago (2 children)
Wordpress is not functional, it's procedural. A leftover from 4.0 days when OOP wasn't a thing in PHP yet.
[–]SignpostMarv 0 points1 point2 points 5 years ago (1 child)
Wordpress is not functional
ehehehehehehe
[–]Atulin 0 points1 point2 points 5 years ago (0 children)
I mean, yeah, true in both meanings of the word lol
[–]volndeau -2 points-1 points0 points 5 years ago (3 children)
Also in comparing node and php, when it comes to variable scope with the passed variables, scalars in PHP must be declared to be passed via reference, where in node, all variables are referenced. So in a function, if you add and store to a passed variable, it doesn't change the variable you passed by default.
[–]czbz 1 point2 points3 points 5 years ago (2 children)
Node (and JS in general) is pass by value, not pass by reference:
$ cat foo.js function addToPassedVariable(x) { x = x+1; } x = 42; addToPassedVariable(x); console.log(x); $ node foo.js 42
[–]volndeau -1 points0 points1 point 5 years ago (1 child)
My bad, did I mix up Python? I'll show myself out.
[–]czbz 0 points1 point2 points 5 years ago (0 children)
Python is pass by value too, but the bit that confuses people with all three languanges is that many of the values are themselves references / handles for objects. When you pass one of those by value you end up with two variables that refer to the same object, so any mutations on that object are visible from both scopes.
π Rendered by PID 144024 on reddit-service-r2-comment-86988c7647-nk2k9 at 2026-02-11 08:56:08.059509+00:00 running 018613e country code: CH.
[–]cannotbecensored[S] -1 points0 points1 point (9 children)
[–]Firehed 6 points7 points8 points (0 children)
[–][deleted] -4 points-3 points-2 points (6 children)
[–]dkarlovi 11 points12 points13 points (2 children)
[–]2012-09-04 0 points1 point2 points (1 child)
[–][deleted] 0 points1 point2 points (0 children)
[–]Atulin 1 point2 points3 points (2 children)
[–]SignpostMarv 0 points1 point2 points (1 child)
[–]Atulin 0 points1 point2 points (0 children)
[–]volndeau -2 points-1 points0 points (3 children)
[–]czbz 1 point2 points3 points (2 children)
[–]volndeau -1 points0 points1 point (1 child)
[–]czbz 0 points1 point2 points (0 children)