But, before you click away and start reading about it, there’s one thing you need to know about V8js: nine times out of ten, you don’t really need it. Sounds confusing, I know. Allow me to explain 🙂
PHP, you see, is a server-side programming language. This means that your PHP app’s code, and all the logic that comes with it, runs in the server.
(If you want to learn more, refer to this article at CloudFlare.)
What You Probably Want to Do Instead
I wrote “probably” in the heading because there’s always—and I mean always—an exception. What I’m about to propose will work for 99% of the people who will read this article. And yet, there will be 1% or more of you who want to do something more sophisticated.
(If you’re wondering why I’m suggesting echo and not print, check out this article at W3Schools.)
To achieve this, you need to write an echo statement with the following three steps:
- Wraps it all in a
This is how the steps above look like in terms of code:
You’re wrapping the function in a
<script> because the Domain Object Model (DOM) expects executable code to be encapsulated in <script> HTML elements. Without it, it will treat the source code as plain text, and the function won’t run.
If you try to call a function that hasn’t been defined, the browser will throw you a “function not defined” error.
Calling a Function That’s Already Defined
Suppose your function has already been defined in your code, and all you need to do is call it.
Were we building the index.php page for a very simple website, where the
<body> are outputted dynamically, and there’s a
<script> in the head with a function that’s defined but not called, our source code would resemble this:
<head> and a call in the
So if you try to do this (notice that the call appears on line 8, and the definition doesn’t appear until line 11):
It won’t work.