This content originally appeared on DEV Community and was authored by Anders Björkland
Just the Gist
Behind the scenes, PHP uses the C programming language to do its magic.
It's C in a nice wrapper 🎁
While we write PHP, behind the scene it is all being interpreted as C. When we write pi()
, what's going on? Behind the scenes it's mapped to this code in the C programming language:
PHP_FUNCTION(pi)
{
ZEND_PARSE_PARAMETERS_NONE();
RETURN_DOUBLE(M_PI);
}
M_PI
is a constant defined in the php_math.h
header file:
#ifndef M_PI
#define M_PI 3.14159265358979323846 /* pi */
#endif
And guess what? We can also call the constant M_PI
from PHP and get the same result. This is really not a surprise, as we can see that the function just returns that constant.
Interpretation skills
So how is <?php echo pi() ?>
able to use the C language to return to us the value of M_PI
? It's because of the Zend Engine. This interpreter has been a part of PHP since version 4, but how does it work? It's a bit like a compiler and a runtime environment rolled up into one:
- It analyzes the code.
- Then it translates it into C.
- And finally it executes the translated code.
☝️ The Zend Engine is a C extension that is part of PHP. You don't have to go chasing after it on the Web if you've already got PHP for your machine. You will have all you need to run your scripts!
What about you?
What are your thoughts on the C programming language? Would you be willing to try it out, maybe even becoming a core contributor? Leave a comment below! ✍
Further Reading
- PHP Internals Book - https://www.phpinternalsbook.com/
- PHP 8.1 Source Code: https://github.com/php/php-src/tree/PHP-8.1.0
- PHP's PI implementation in C:
- Compiled vs interpreted languages: https://www.freecodecamp.org/news/compiled-versus-interpreted-languages/
This content originally appeared on DEV Community and was authored by Anders Björkland
Anders Björkland | Sciencx (2021-12-03T06:55:17+00:00) Do they know it’s C in PHP 🎄. Retrieved from https://www.scien.cx/2021/12/03/do-they-know-its-c-in-php-%f0%9f%8e%84/
Please log in to upload a file.
There are no updates yet.
Click the Upload button above to add an update.