The "function" you have is server-side. Server-side code runs before and only before data is returned to your browser (typically, displayed as a page, but also could be an ajax request).

The form you have is client-side. This form is rendered by your browser and is not "connected" to your server, but can submit data to the server for processing.

Therefore, to run the function, the following flow has to happen:

  1. Server outputs the page with the form. No server-side processing needs to happen.
  2. Browser loads that page and displays the form.
  3. User types data into the form.
  4. User presses submit button, an HTTP request is made to your server with the data.
  5. The page handling the request (could be the same as the first request) takes the data from the request, runs your function, and outputs the result into an HTML page.

Here is a sample PHP script which does all of this:

<?php

function addNumbers($firstNumber, $secondNumber) {
    return $firstNumber + $secondNumber;
}

if (isset($_POST['number1']) && isset($_POST['number2'])) {
    $result = addNumbers(intval($_POST['number1']), intval($_POST['number2']));
}
?>
<html>
<body>

    <?php if (isset($result)) { ?>
        <h1> Result: <?php echo $result ?></h1>
    <?php } ?>
    <form action="" method="post">
    <p>1-st number: <input type="text" name="number1" /></p>
    <p>2-nd number: <input type="text" name="number2" /></p>
    <p><input type="submit"/></p>

</body>
</html>

Please note:

  • Even though this "page" contains both PHP and HTML code, your browser never knows what the PHP code was. All it sees is the HTML output that resulted. Everything inside <?php ... ?> is executed by the server (and in this case, echo creates the only output from this execution), while everything outside the PHP tags โ€” specifically, the HTML code โ€” is output to the HTTP Response directly.
  • You'll notice that the <h1>Result:... HTML code is inside a PHP if statement. This means that this line will not be output on the first pass, because there is no $result.
  • Because the form action has no value, the form submits to the same page (URL) that the browser is already on.
Answer from Nicole on Stack Overflow
Top answer
1 of 7
28

The "function" you have is server-side. Server-side code runs before and only before data is returned to your browser (typically, displayed as a page, but also could be an ajax request).

The form you have is client-side. This form is rendered by your browser and is not "connected" to your server, but can submit data to the server for processing.

Therefore, to run the function, the following flow has to happen:

  1. Server outputs the page with the form. No server-side processing needs to happen.
  2. Browser loads that page and displays the form.
  3. User types data into the form.
  4. User presses submit button, an HTTP request is made to your server with the data.
  5. The page handling the request (could be the same as the first request) takes the data from the request, runs your function, and outputs the result into an HTML page.

Here is a sample PHP script which does all of this:

<?php

function addNumbers($firstNumber, $secondNumber) {
    return $firstNumber + $secondNumber;
}

if (isset($_POST['number1']) && isset($_POST['number2'])) {
    $result = addNumbers(intval($_POST['number1']), intval($_POST['number2']));
}
?>
<html>
<body>

    <?php if (isset($result)) { ?>
        <h1> Result: <?php echo $result ?></h1>
    <?php } ?>
    <form action="" method="post">
    <p>1-st number: <input type="text" name="number1" /></p>
    <p>2-nd number: <input type="text" name="number2" /></p>
    <p><input type="submit"/></p>

</body>
</html>

Please note:

  • Even though this "page" contains both PHP and HTML code, your browser never knows what the PHP code was. All it sees is the HTML output that resulted. Everything inside <?php ... ?> is executed by the server (and in this case, echo creates the only output from this execution), while everything outside the PHP tags โ€” specifically, the HTML code โ€” is output to the HTTP Response directly.
  • You'll notice that the <h1>Result:... HTML code is inside a PHP if statement. This means that this line will not be output on the first pass, because there is no $result.
  • Because the form action has no value, the form submits to the same page (URL) that the browser is already on.
2 of 7
5

Try This.

  <?php 
            function addNumbers($firstNumber, $secondNumber)
            {
            if (isset($_POST['number1']) && isset($_POST['number2']))
            {
                $firstNumber = $_POST['number1'];
                $secondNumber = $_POST['number2'];
                $result = $firstNumber + $secondNumber;
                    echo $result;
            }

            }
    ?>

            <form action="urphpfilename.php" method="post">
            <p>1-st number: <input type="text" name="number1" /></p>
            <p>2-nd number: <input type="text" name="number2" /></p>
            <?php addNumbers($firstNumber, $secondNumber);?>
            <p><?php echo $result; ?></p>
            <p><input type="submit"/></p>
๐ŸŒ
W3Schools
w3schools.com โ€บ php โ€บ php_functions.asp
PHP Functions
Well organized and easy to understand Web building tutorials with lots of examples of how to use HTML, CSS, JavaScript, SQL, Python, PHP, Bootstrap, Java, XML and more.
Discussions

HTML within PHP functions
So, first off, the easy stuff. Don't end your files with a closing tag. Secondly, this is a terrible practice. It's hard to read It's surprising that a function call would result in output It starts output buffering which is probably not wanted. If you're mixing PHP and HTML, you should be using the alternative syntax. This mixing of PHP and HTML is very old school and really should be avoided. This would be preferable to me: Hello World!

'; } echo my_example(); If you absolutely must mix PHP and HTML on the page, do it like this:

Ideally functions shouldn't have side-effects (ie, they shouldn't be outputting stuff) they should do some work and return a result. I strongly encourage you to read: https://phptherightway.com/ . In particular: https://phptherightway.com/#templating More on reddit.com
๐ŸŒ r/PHPhelp
14
3
May 18, 2021
How do I add HTML to a PHP function - WordPress Development Stack Exchange
Generic PHP/JS/SQL/HTML/CSS questions might be better asked at Stack Overflow or another appropriate Stack Exchange network site. Third-party plugins and themes are off-topic for this site; they are better asked about at their developers' support routes. Closed 7 years ago. ... add_action( 'init', 'wc_readd_add_to_cart_buttons' ); function ... More on wordpress.stackexchange.com
๐ŸŒ wordpress.stackexchange.com
Is inline HTML valid inside a PHP function? - Stack Overflow
Is the following valid PHP? (I know this is not a good idea, just want to know if it's possible.) More on stackoverflow.com
๐ŸŒ stackoverflow.com
Calling a PHP function from an <a> tag? - PHP - SitePoint Forums | Web Development & Design Community
Iโ€™m new to PHP and was wondering if itโ€™s possible to call a PHP function from an tag in HTML. Below is my code that Iโ€™m using. More on sitepoint.com
๐ŸŒ sitepoint.com
0
July 28, 2018
๐ŸŒ
Reddit
reddit.com โ€บ r/phphelp โ€บ html within php functions
r/PHPhelp on Reddit: HTML within PHP functions
May 18, 2021 -

I`m fairly new to PHP and started learning in January. Here`s a simple question that has been bugging me.

Title is a bit vague so let me explain.Code that is not echo`ed out is not visible for the visitor of the page.However, plain written HTML is visible. This leaves me wondering if I am breaking any rules or coding conventions when I write HTML like this.In the PHP function I end the PHP tag, write HTML and then start the PHP tag in the end of the function. Example:

<?php
    function my_example() {
        ?>
        <p>Hello World!</p>
        <?php
    }
?>

As you see in the code above, the function my_example will spit out Hello World! even though I do not echo out the HTML code. However, it will (as far as I know) not be visible in the source for the visitor unless this function is called for.

I hope this is not a stupid question, but I feel like asking it anyway.Are there any negative implications of doing it this way and or are there security issues of having code being written like this?

๐ŸŒ
Quora
quora.com โ€บ How-do-I-call-PHP-functions-from-HTML-code
How to call PHP functions from HTML code - Quora
You can't call PHP directly from HTML - PHP is only run on the server, typically to generate HTML.
๐ŸŒ
Medium
medium.com โ€บ @brajagopal.tripathi โ€บ how-to-call-php-functions-from-html-code-fa22230b1274
How to call PHP functions from HTML code? | by Brajagopal Tripathi | Medium
September 26, 2023 - To use the PHP short tag, you would start your PHP code with <? and end it with ?>. For example, the following HTML code would call the PHP function myFunction() and display the result:
Find elsewhere
๐ŸŒ
Quora
quora.com โ€บ How-do-you-use-HTML-inside-a-functions-PHP-code-PHP-plugin-development-functions-WordPress
How to use HTML inside a functions.PHP code (PHP, plugin development, functions, WordPress) - Quora
Answer (1 of 3): The easiest way to use HTML inside a PHP function is to treat the markup just like you would with any PHP string. function getHTMLCode(){ $html=โ€™ This is how you can add HTML inside PHP โ€™; return $html; } As you can see, ...
๐ŸŒ
W3Docs
w3docs.com โ€บ php
How to run a PHP function from an HTML form?
<?php $input = $_POST["input"]; myFunction($input); ?> ... You need to make sure that the function is defined before it is called , otherwise you will get a fatal error .
๐ŸŒ
SitePoint
sitepoint.com โ€บ php
Calling a PHP function from an <a> tag? - PHP - SitePoint Forums | Web Development & Design Community
July 28, 2018 - Iโ€™m new to PHP and was wondering if itโ€™s possible to call a PHP function from an <a> tag in HTML. Below is my code that Iโ€™m using. <div class="menuBar"> <nav> <label classโ€ฆ
๐ŸŒ
PHP
php.net โ€บ manual โ€บ en โ€บ function.htmlentities.php
PHP: htmlentities - Manual
htmlentities( string $string, int $flags = ENT_QUOTES | ENT_SUBSTITUTE | ENT_HTML401, ?string $encoding = null, bool $double_encode = true ): string ยท This function is identical to htmlspecialchars() in all ways, except with htmlentities(), all characters which have HTML character entity equivalents are translated into these entities.
๐ŸŒ
Team Treehouse
teamtreehouse.com โ€บ community โ€บ return-html-from-a-php-function
return html from a php function (Example) | Treehouse Community
January 21, 2017 - <?php $jobs = array( array('id'=>'LW12345','MK'=>1,'jobTitle'=>'C4BH35','jobStartDate' => date("d/m/Y"),'qty'=>5000,'jobDuration'=>'3:00'), array('id'=>'LW15783','MK'=>1,'jobTitle'=>'C4BH75','jobStartDate' => date("d/m/Y",strtotime("+ 1 day")),'qty'=>12000,'jobDuration'=>'3:00'), array('id'=>'DG12345','MK'=>1,'jobTitle'=>'C5BH50','jobStartDate' => date("d/m/Y",strtotime("+ 2 day")),'qty'=>6000,'jobDuration'=>'3:00'), array('id'=>'LK17543','MK'=>1,'jobTitle'=>'C5BH35','jobStartDate' => date("d/m/Y",strtotime("+ 3 day")),'qty'=>9000,'jobDuration'=>'3:00') ); function machineJobs($jobs) { $output
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ php โ€บ how-to-call-php-function-on-the-click-of-a-button
How to call PHP Function on the Click of a Button? - GeeksforGeeks
July 12, 2025 - The POST method describes how to send data to the server. After clicking the button, the array_key_exists() function is called. Example 1: This example shows the use of the above-explained approach. ... <!DOCTYPE html> <html> <head> <title> How to call PHP function on the click of a Button ?
๐ŸŒ
phpBB
phpbb.com โ€บ board index โ€บ non-support specific โ€บ phpbb discussion โ€บ phpbb custom coding
phpBB โ€ข How Call an PHP function in HTML pages of template?
February 24, 2018 - Under secutity settings enable PHP in templates. One thing to be aware of is your PHP code can be exposed because the template can be requested directly and it is sent plain text. {USERNAME} In functions.php you'll find a giant array near the bottom.
๐ŸŒ
Sololearn
sololearn.com โ€บ en โ€บ Discuss โ€บ 69988 โ€บ you-can-call-a-php-function-from-html-have-include
you can call a php function from html have include? | Sololearn: Learn to code for FREE!
October 10, 2016 - This defintely works for me: <?php yourFunction() { $var = "whatever you want to do in this funct"; } ?> <!DOCTYPE html> <html> <body> <h1>Welcome to my home page!</h1> <p>Some text.</p> <p>Some more text.<?php yourFunction(); ?></p> </body> </html> BTW....you have to save your file as .php.
๐ŸŒ
Quora
codecamp.quora.com โ€บ How-to-call-PHP-functions-from-HTML-code
How to call PHP functions from HTML code - CodeCamp - Quora
Answer: To call PHP functions from HTML code, you need to embed the PHP code within the HTML using the PHP tags [code ] [/code]. For example: [code]htmlCopy code [/code]This will execute the [code ]myFunction()[...