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:
- Server outputs the page with the form. No server-side processing needs to happen.
- Browser loads that page and displays the form.
- User types data into the form.
- User presses submit button, an HTTP request is made to your server with the data.
- 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,echocreates 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 PHPifstatement. This means that this line will not be output on the first pass, because there is no$result. - Because the form
actionhas no value, the form submits to the same page (URL) that the browser is already on.
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:
- Server outputs the page with the form. No server-side processing needs to happen.
- Browser loads that page and displays the form.
- User types data into the form.
- User presses submit button, an HTTP request is made to your server with the data.
- 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,echocreates 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 PHPifstatement. This means that this line will not be output on the first pass, because there is no$result. - Because the form
actionhas no value, the form submits to the same page (URL) that the browser is already on.
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>
HTML within PHP functions
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
How do I add HTML to a PHP function - WordPress Development Stack Exchange
Is inline HTML valid inside a PHP function? - Stack Overflow
Calling a PHP function from an <a> tag? - PHP - SitePoint Forums | Web Development & Design Community
Videos
You can use a heredoc, which supports variable interpolation, making it look fairly neat:
function TestBlockHTML ($replStr) {
return <<<HTML
<html>
<body><h1>{$replStr}</h1>
</body>
</html>
HTML;
}
Pay close attention to the warning in the manual though - the closing line must not contain any whitespace, so can't be indented.
Yes, there is: you can capture the echoed text using ob_start:
<?php function TestBlockHTML($replStr) {
ob_start(); ?>
<html>
<body><h1><?php echo($replStr) ?></h1>
</html>
<?php
return ob_get_clean();
} ?>
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?
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
You can use 'echo' (or 'print') to enclose HTML, but sometimes that gets a bit messy with complex HTML, not to mention having to escape quote/double-quote character.
So try something like this:
function myfunction() {
// after this next, plain HTML
?>
<div class='myclass'><h1 align="center">This is a heading</h1></div>
<!-- more HTML code here -->
<?php // back to PHP
// .. some more PHP stuff
return;
}
That allows you to put in some complex HTML (or a bunch of it) without having to use echo/print.
Not sure why you are using init and then adding the function to the WC action. The following should work:
add_action( 'woocommerce_after_shop_loop_item', 'wc_readd_add_to_cart_buttons', 10 );
if (!function_exists('wc_readd_add_to_cart_buttons')){
function wc_readd_add_to_cart_buttons() {
//add to cart button loop
echo "<br />";
woocommerce_template_loop_add_to_cart();
}
}
<?php
function a($a) {
?>
<p><? echo $a?></p> //echo variable content's
// <? ?> is allowed only if you have **Enabled short_tages in php.ini file**
<?php
}
?>
Enable short_tages in php.ini file
<?
$a="stackoverflow";
function a($a) {
?>
<p><?= echo $a?></p>
<?
}
a($a);
?>
If you try to run this program using <?= this won't allowed it will give you error
Parse error: syntax error, unexpected T_ECHO
<?= is not allowed in php for tags <? is allowed if Set
short_open_tag=On
in php.ini
As of PHP 5.4.0, <?= ?> PHP tags are available regardless of short_open_tag ini setting.
If short_open_tags are on, yes, it's possible.