how to call php function in html code example
Example 1: call javascript function from php
<?php
echo '<script type="text/javascript">',
'someJsFunc();',
'</script>';
?>
Example 2: how to define function in php
<?php
function writeMsg() {
echo "Hello world!";
}
writeMsg();
?>
Example 3: call php function in js
<script>
var phpadd= <?php echo add(1,2);?>
var phpmult= <?php echo mult(1,2);?>
var phpdivide= <?php echo divide(1,2);?>
</script>
Example 4: call php from html
<form action="myphpfile.php">
<input type="submit" value="click on me!">
</form>
Example 5: php functions
#functions
<?php
#function - a block of code that can be repeatedly called
function simpleFunction(){
echo 'Hello John';
}
simpleFunction();
function sayHello($name = " you out there!"){
echo "<br>and<br> Hello $name<br>";
}
sayHello('John');
sayHello();
function addNumbers($num1, $num2){
return $num1 + $num2;
}
echo addNumbers(2,3);
$myNum = 10;
function addFive($num){
$num += 5;
}
function addTen(&$num) {
$num += 10;
}
addFive($myNum);
echo "<br>Value: $myNum<br>";
addTen($myNum);
echo "Value: $myNum<br>";
?>