Current Location: Home> Latest Articles> How to Quickly Locate and Fix “Undefined Function” Error When Calling cosh in PHP?

How to Quickly Locate and Fix “Undefined Function” Error When Calling cosh in PHP?

gitbox 2025-09-19

1. Check the PHP Version

cosh() is a built-in function available since PHP 4.0.0. If your PHP version is too old, it may not support this function. You can check your current PHP version with the following code:

<span><span><span class="hljs-meta">&lt;?php</span></span><span>  
</span><span><span class="hljs-keyword">echo</span></span><span> </span><span><span class="hljs-string">&#039;Current PHP version: &#039;</span></span><span> . </span><span><span class="hljs-title function_ invoke__">phpversion</span></span><span>();  
</span><span><span class="hljs-meta">?&gt;</span></span><span>  
</span></span>

If the version is too low, it is recommended to upgrade PHP to a newer version (at least PHP 5.6+).


2. Check if the Math Extension is Enabled

cosh() belongs to PHP’s math extension (ext-bcmath or ext-math is not strictly required, but some PHP builds may require enabling the php_math extension). In rare cases, a minimal or custom-compiled PHP installation may exclude this function.

You can use function_exists to check:

<span><span><span class="hljs-meta">&lt;?php</span></span><span>  
</span><span><span class="hljs-keyword">if</span></span><span> (</span><span><span class="hljs-title function_ invoke__">function_exists</span></span><span>(</span><span><span class="hljs-string">&#039;cosh&#039;</span></span><span>)) {  
    </span><span><span class="hljs-keyword">echo</span></span><span> </span><span><span class="hljs-string">"cosh function is available"</span></span><span>;  
} </span><span><span class="hljs-keyword">else</span></span><span> {  
    </span><span><span class="hljs-keyword">echo</span></span><span> </span><span><span class="hljs-string">"cosh function is undefined, you may need to enable the math extension"</span></span><span>;  
}  
</span><span><span class="hljs-meta">?&gt;</span></span><span>  
</span></span>

If it returns “undefined,” you can enable it as follows:

  • Check php.ini for extension=php_math.dll (Windows) or ensure the math extension is installed on Linux.
  • Restart your web server (e.g., Apache, Nginx+PHP-FPM).

3. Ensure There Are No Naming Conflicts

Sometimes developers may accidentally define their own cosh function, or mistakenly call it within a namespace, which overrides or hides the built-in function. Check if your code has something like this:

<span><span><span class="hljs-function"><span class="hljs-keyword">function</span></span></span><span> </span><span><span class="hljs-title">cosh</span></span><span>(</span><span><span class="hljs-params"><span class="hljs-variable">$x</span></span></span><span>) {  
    </span><span><span class="hljs-keyword">return</span></span><span> </span><span><span class="hljs-variable">$x</span></span><span>; </span><span><span class="hljs-comment">// Wrong example</span></span><span>  
}  
</span></span>

If so, rename your custom function to avoid conflicts with the built-in one.


4. Temporary Workaround

If you can’t modify your PHP configuration right away, you can manually implement the cosh function:

<span><span><span class="hljs-meta">&lt;?php</span></span><span>  
</span><span><span class="hljs-keyword">if</span></span><span> (!</span><span><span class="hljs-title function_ invoke__">function_exists</span></span><span>(</span><span><span class="hljs-string">&#039;cosh&#039;</span></span><span>)) {  
    </span><span><span class="hljs-function"><span class="hljs-keyword">function</span></span></span><span> </span><span><span class="hljs-title">cosh</span></span><span>(</span><span><span class="hljs-params"><span class="hljs-variable">$x</span></span></span><span>) {  
        </span><span><span class="hljs-keyword">return</span></span><span> (</span><span><span class="hljs-title function_ invoke__">exp</span></span><span>(</span><span><span class="hljs-variable">$x</span></span><span>) + </span><span><span class="hljs-title function_ invoke__">exp</span></span><span>(-</span><span><span class="hljs-variable">$x</span></span><span>)) / </span><span><span class="hljs-number">2</span></span><span>;  
    }  
}  
</span><span><span class="hljs-meta">?&gt;</span></span><span>  
</span></span>

This ensures your code runs in any environment, but it’s better to resolve the root cause by enabling the math extension.