5

Do they know it's C in PHP 🎄

 2 years ago
source link: https://dev.to/andersbjorkland/do-they-know-its-c-in-php-4ojh
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

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);
}

Enter fullscreen mode

Exit fullscreen mode

M_PI is a constant defined in the php_math.h header file:

#ifndef M_PI
#define M_PI           3.14159265358979323846  /* pi */
#endif

Enter fullscreen mode

Exit fullscreen mode

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:

  1. It analyzes the code.
  2. Then it translates it into C.
  3. 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


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK