What is USE in php? - php

What does USE do in php? I thought it was like include() but apparently I'm wrong. Could somebody point me to the php documentation on USE and maybe some example code?

use has two uses:
Namespaces
Anonymous Functions, aka Closures

Related

How can I view definitions of PHP functions?

I know PHP's native code is compiled and what we write is interpreted.
Is there a way I can view definitions of inbuilt functions like isset(), strlen(), strrev(), etc?
I use LAMP.
You can look at PHP's source code. Example: strrev.
http://www.php.net will show you all the functions needed.

Backslash in PHP -- what does it mean?

I just saw the use of a backslash in a reference to a PHP object and was curious about it (I have never seen this before). What does it mean?
$mail = new SendGrid\Mail();
If you're curious, here's SendGrid's documentation.
It's because they're using PHP namespaces. Namespaces are new as of PHP 5.3.
It's PHP's namespace operator: http://php.net/manual/en/language.namespaces.php.
Don't ask why it's a backslash. It's (imho) the stupidest possible choice they could have made, basing their decisions on a highly slanted/bigoted scoring system that made sense only to the devs.
This is syntax for namespaces. You can read more about namespaces at PHP documentation. They they require at least PHP 5.3.
For example:
namespace SendGrid;
function Mail() {
// You can access this function by using SendGrid\Mail() externally
}

PHP new keyword with directory?

I am looking at this tutorial:
http://www.joelverhagen.com/blog/2011/05/setting-up-codeigniter-2-with-doctrine-2-the-right-way/
and I see the new keyword used like so:
$doctrineClassLoader = new \Doctrine\Common\ClassLoader('Doctrine', APPPATH.'libraries');
I am not used to seeing the new keyword used with what appears to be a directory or something? I was looking through the PHP manual to find similar usage and what this means (and how it works) but didn't see what I was looking for.
Can someone explain this usage to me or point me to example code in the PHP manual.
PHP 5.3 introduced namespaces. Namespace are another way of structuring your program and the library you are working with is using them extensively. You can basically put classes and functions inside a namespace. Namespaces can be nested. In order to use them you prefix every namespace by a backslash.
You can read more about namespaces in the PHP manual.

php I need to write a json_encode function equivalent

I'm trying to use webspace provided by my university. They are currently using an outdated version of php, 5.1 I think. Anyway it doesn't have a json_encode function, however, I need a json_encode function (or equivalent) for my code to work.
So if anyone could explain to me the syntax for the return of the json_encode function or point me to a website that explains it nicely it'd be much appreciated.
Thanks
Don't reinvent the wheel: upgrade.php library.
http://pear.php.net/package/Services_JSON
Check here. It tells you to use require 'jsonwrapper.php'; at the start of your code in versions of PHP where json_encode is not available.

php e() and h() functions?

I and lately I'm seeing h() and e() functions in PHP. I have googled them, but they are so short that results don't give any idea of what they are. I got results like exponential or math related functions. For example:
<td><?php echo h($room['Room']['message']) ?></td>
Does anyone have an idea? or maybe they are not called functions? (I think I read about that very long ago, but I can remember its real name)
ADDED:
Thanks, for the replies. I am using CakePHP and also found an e() example:
<?php e($time->niceShort($question['Question'] ['created'])) ?>
If they were escaping somehow strings I think it would make sense, since I always see them right next the "echo"
I still don't know what they are ;(
As several readers have said, these are CakePHP-specific short-cuts. You can find them in the API docs at: here (for CakePHP 2.x)
I think I read that some of these are going to be removed in 1.3, personally I never used e() as typing echo really doesn't take that much longer :)
edit: e() is deprecated in 1.3 and no longer available in 2.0 see here
It looks like it might be CakePHP.
See e()
e (mixed $data)
Convenience wrapper for echo().
This has been Deprecated and will be removed in 2.0 version. Use
echo() instead.
See h()
h (string $text, string $charset = null)
Convenience wrapper for htmlspecialchars().
Most likely, they are dummy functions someone introduced for the sake of brevity. The h(), for example, looks like an alias for htmlspecialchars():
function h($s)
{
return htmlspecialchars($s);
}
So look for them in the include files. Espec. the ones with names likes "util.php" or "lib.php".
Likely the framework you're using is doing some escaping and has defined some short hands for htmlentities and htmlspecialchars or equivalents.
I'd do a search on whatever framework you're using for "function h("
They are probably functions defined and implemented by the group's code that you're looking at. I'm not aware of any e/h functions in the PHP language.
Nothing here:
http://us3.php.net/manual/en/function.h.php
http://us3.php.net/manual/en/function.e.php
There aren't any functions in PHP called h() and e(). They must be declared in the project you are working on. search for them and find out what they do.
In CakePHP h() is:
Convenience wrapper for htmlspecialchars()
For more information about Global constants and functions in CakePHP view this link
http://book.cakephp.org/2.0/en/core-libraries/global-constants-and-functions.html
I'd guess that h() escapes user-submitted data for safe output, and e() escapes for database insertion. Whatever the functionality, these are not stock PHP functions.
It's CakePHP.
echo h('some stuff')
Is just htmlspecialchar()ing the stuff.
If you are using a decent editor press ctrl and click on the function. It should take you to the function's declaration.
http://book.cakephp.org/view/121/Global-Functions these are shortcut functions in cakePHP
Many of them are deprecated in 1.3 so beware of using them yourself
h() is global function in CakePHP. Documents about h() for CakePHP version 2.5.7 : http://book.cakephp.org/2.0/en/core-libraries/global-constants-and-functions.html#global-functions
Laravel also use e() helper function to runs htmlentities over the given string.
echo e('<html>foo</html>');
// <html>foo</html>
documentation : https://laravel.com/docs/5.8/helpers#method-e

Categories