Title: Php critical error
Last modified: June 18, 2020

---

# Php critical error

 *  Resolved [danielm04](https://wordpress.org/support/users/danielm04/)
 * (@danielm04)
 * [5 years, 11 months ago](https://wordpress.org/support/topic/php-critical-error-2/)
 * Hello there, i need help with my code, in the tryit editor on w3shcools it works,
   but when i paste it on the php block on my website, the website shows me a critical
   error, can anyone help me with this?
 *     ```
        <script>
             function myfunction() {
           if(1+1=2) { 
               <?php $src= "myimg2.png"?>
       } 
           else {
               <?php $src= "myimg.png"?>
            } 
       };
           </script>
           <img width="60" class="image2" src="<?php echo $src?>" />
       ```
   
    -  This topic was modified 5 years, 11 months ago by [danielm04](https://wordpress.org/support/users/danielm04/).
    -  This topic was modified 5 years, 11 months ago by [danielm04](https://wordpress.org/support/users/danielm04/).

Viewing 2 replies - 1 through 2 (of 2 total)

 *  [RossMitchell](https://wordpress.org/support/users/rossmitchell/)
 * (@rossmitchell)
 * [5 years, 11 months ago](https://wordpress.org/support/topic/php-critical-error-2/#post-13003032)
 * Several things:
    – Inside a “script” block you do not use `<?php`, it is all 
   assumeb to be PHP – You are defining a function named “myfunction”, but the code
   will not be executed until the function is invoked. And then the variable “$src”
   is local to the function at the moment. – note addition of various ; and other
   stuff. – note that within double quote strings that php variable are replaced.
   What you presumably need is more like, (NB: this may not work but is an improvement):
 *     ```
        <script>
       $src = "needs to be set somehow";
       function myfunction() {
           global $src;
           if(1+1=2) { 
               $src= "myimg2.png";
           } else {
               $src= "myimg.png";
           } 
       };
           </script>
           <?php myfunction(); ?>
           <img width="60" class="image2" src="$src" />
       ```
   
    -  This reply was modified 5 years, 11 months ago by [RossMitchell](https://wordpress.org/support/users/rossmitchell/).
 *  Thread Starter [danielm04](https://wordpress.org/support/users/danielm04/)
 * (@danielm04)
 * [5 years, 11 months ago](https://wordpress.org/support/topic/php-critical-error-2/#post-13009768)
 * In case someone has a problem like this one, i’ve been testing some things and
   the solution is this:
 *     ```
        <script>
       function myfunction() {
           if(1+1=2) { 
               document.getElementById("image2").src= "myimg2.png";
           } else {
               document.getElementById("image2").src= "myimg.png";   
        } };
           </script>
           <img width="60" src="" />
       ```
   
    -  This reply was modified 5 years, 11 months ago by [bcworkz](https://wordpress.org/support/users/bcworkz/).
      Reason: code fixed

Viewing 2 replies - 1 through 2 (of 2 total)

The topic ‘Php critical error’ is closed to new replies.

## Tags

 * [php](https://wordpress.org/support/topic-tag/php/)

 * In: [Developing with WordPress](https://wordpress.org/support/forum/wp-advanced/)
 * 2 replies
 * 2 participants
 * Last reply from: [danielm04](https://wordpress.org/support/users/danielm04/)
 * Last activity: [5 years, 11 months ago](https://wordpress.org/support/topic/php-critical-error-2/#post-13009768)
 * Status: resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
