21
Do they know it's C in PHP 🎄
Just the Gist
PHP uses the C programming language to become what it is.Correction: Earlier version of this article stated that the Zend Engine took PHP and interpreted it to C. This is not the case. The PHP source is compiled from C, the Zend engine compiles PHP into Opcodes that can be executed by the Zend VM.
While we write PHP, our basic features are provided to us from compiled C code. When we write pi()
, where did that come from? Before it was compiled for us, its origin is from 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.
So what is the journey for <?php echo pi() ?>
to an output of 3.1415926535898
? The source of PHP was compiled into a package we know as PHP, where pi()
and M_PI
are included. This is compiled at runtime into Opcodes
by the Zend Engine
. Opcodes
are interpreted and executed by the engine. It's a bit like a compiler and a runtime environment rolled up into one:
- It analyzes the code.
- Then it compile it into Opcodes.
- And finally it executes the compiled 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 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! ✍
- 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/
21