call php function from html code example

Example 1: call javascript function from php

// The most basic method
<?php
echo '<script type="text/javascript">',
     'someJsFunc();', // Or Whatever
     '</script>';
?>
  
// However, if what you are trying to achieve requires more complexity,
// You might be better off adding the V8JS module, see link below

Example 2: call php function in js

<script>
  var phpadd= <?php echo add(1,2);?> //call the php add function
  var phpmult= <?php echo mult(1,2);?> //call the php mult function
  var phpdivide= <?php echo divide(1,2);?> //call the php divide function
</script>

Example 3: call php from html

<form action="myphpfile.php">
  <input type="submit" value="click on me!">
</form>

Example 4: execute function php

function functionName() {
    //code to be executed;
}
functionName();

Example 5: php functions

#functions

<?php
    #function - a block of code that can be repeatedly called

    /*
    How to format functions
    1. Camel Case myFunction()
    2.Lower case with underscore my_function()
    3. Pascal Cae - MyFunction() usally used with classes
    */
    function simpleFunction(){
        echo 'Hello John';

    }
    //Run the function like so
    simpleFunction();

    //function with param
    function sayHello($name = " you out there!"){
        echo "<br>and<br> Hello $name<br>";
    }
    sayHello('John');
    sayHello();

    //Reurn Value
    function addNumbers($num1, $num2){
        return $num1 + $num2;
     }
     echo addNumbers(2,3);

     // By Reference

     $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>";


?>