What's the difference between using PHP define constant and PHP $_GLOBALS? [closed] - php

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 2 years ago.
Improve this question
I read somewhere that defining constants with PHP's define function like so:
define('BASE_PATH','/var/www/html/example/');
is better and more secure than storing the same variable data inside a globals variable like so:
$_GLOBALS['BASE_PATH'] = '/var/www/html/example/';
Could somebody please explain the difference, which is better in which scenarios, and why?
I've just read here:
PHP Manual - The 'define' function
That the 'define' function can cause unwanted oddities.
a) What are the security implications of both?
b) How does PHP manage and store each of the variable's data in physical memory?

Related

Passing a variable through href - is it a good approach? [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 5 years ago.
Improve this question
I'm currently working on my school project and I started wondering if it's a good approach to pass some variable through href so it can been seen in the URL.
Is it good or a bad way and why?
Or are there any better ways?
If it's not information that should be private I don't see why you couldn't do it. Especially if you need to reference something on a new page, it would make things easier.
It is good practice to use GET parameters for navigation purposes.
For user data input you can better use forms with POST.

When to use PHP's variable variables? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
I've been using PHP for some basics Back-End development for a while now. I saw something about interpretations of variables while I was looking for some changes which came with PHP 7. I'm not using them and it would be great if someone can explain why to use them?
What I mean is:
What are the pros of using them?
You use variable interpretation in situations when you need to dynamically reference a variable and don't want to use an array. Generally I would not recommend using it, as you lose benefits such as static code analysis.

Why does PHP need the use operator for closures? [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 7 years ago.
Improve this question
I know what the use operator is doing in something like function($x,$y) use ($z) { ...
What I don't understand is why PHP uses this construction when other languages don't?
Javascript has rather loose variable scoping (you don't need to declare variables as global). PHP has tighter variable scoping (if a variable isn't defined within the scope that it's used, and isn't brought in with global, then it doesn't exist).
The use declaration tells PHP to make those variables available within the closure (and likely also tells the garbage collector not to clean them up until after the closure gets cleaned up).

PHP: is it mandatory to unset objects you create via "new"? [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 7 years ago.
Improve this question
If during a function, I new AnObject - do I have to unset it before the function exist? or is it done automatically using reference counting?
The garbage collection should take care of it for you:
PHP performs garbage collection at three primary junctures:
When you tell it to
When you leave a function
When the script ends
So no, you should not have to unset anything, but you can if you want to.
When unsettling a variable it seems that you only are marking the value for the garbage collector. And when you're exiting your function's scope, the reference counter will be decremented, and your object will be marked to be deleted, having the same result that you would have calling the unset.
https://php.net/manual/en/features.gc.refcounting-basics.php

Is using the indirection operator twice in a statement a good practise? [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 7 years ago.
Improve this question
Sometimes I see things like this:
<?php echo $this->getLayout()->createBlock("menupro/menu")->setGroup_id(5)->setTemplate("menupro/menupro.phtml")->toHtml(); ?>
I was wondering whether this is a good practise?
This is a rather subjective question, I personally am all for it. You can make your code a lot more readable. Check out this link:
Effects of method chaining
The major drawback is that you must return the object. You may not return any other value as you’re only allowed to return $this.

Categories