How can I view definitions of PHP functions? - php

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.

Related

Removing var_dump from PHP code

We have a large codebase, and every so often a var_dump used for testing and not removed/commented suddenly appears out of nowhere. There is a messy solution using XDebug (http://devzone.zend.com/1135/tracing-php-applications-with-xdebug/), but maybe there's something ingenous that can be done in PHP at runtime.
Also, I don't want to modify or search code via regex. I've tried using my own var_dump_v2, but it falls out of use quickly.
Is it possible to use the disable_functions operation in php.ini to disable var_dump on your production server? I am not sure what the outcome of this setting is (ie does it fail with an error, or silently) the documentation is not so clear.
http://php.net/manual/en/ini.core.php - see "disable_functions"
Also there is override_function:
<?php
override_function('var_dump', '$a', 'return 0;');
?>
http://php.net/manual/en/function.override-function.php
There are actually ways to do this, if you have PECL available and runkit installed. You kan make runkit able to overide PHPs internal functions if you in php.ini set runkit.internal_override to "1".
For removing the var_dump function, you could use:
runkit_function_remove('var_dump');
In your case, not to get an error, you should probably instead use something like this:
runkit_function_redefine('var_dump', '','');
Take a look at the runkit extensions documentation here.
You may also want to take a look at "Advanced PHP debugger", another extension that seems to offer an override_function().
You can use monkey patching.
Just defines a namespace on the first line of your file and defines the function var_dump
<?php
namespace monkey;
function var_dump($obj) {}
Of course, it implies that you do not use a namespace in your current file
You could use the function var_dump() prefixing it with the root namespace(): \var_dump()
Of course, all others native function will continue to work as usual as long as you do not override them in your namespace.
Why don't you use serialize() or json_encode() if you have a large database? That will be very useful.
But take note, serialize() will give you a 1-line output somewhat like this:
's:0:"";s:5:"value";'
So you need to learn the anatomy of serialize() to use it: PHP Serialize

What is USE in 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

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

My php can't find 'normalizer_normalize()' function, why?

How come I can't use normalizer function?
I am using it exactly as I should, ie: $var = normalizer_normalize($str);
I get php error: no such function!
Here is the code:
$headline= mysql_real_escape_string($_POST['headline']);
$ad_id_string=normalizer_normalize($headline);
Thanks
From the docs, normalizer_normalize was included in PHP as of version 5.3.0. Prior to that, it was available as a PECL extension. To build it as part of PHP, you'll need to install internationalization support.
If you need to provide your own equivalent to the Normalizer class, strtr() would be a good option. The only downside is that you will need to provide your own character strings to translate to / from.

Categories