I have script which has the following structure:
<?php
if( a == b ){
function some_function_name(){
// do something here
}
if( c == d ){
some_function_name();
}
}
?>
I get an error message saying that some_function_name(); does not exist.
What am I doing wrong and is this good practice?
I want to create a function, because I have a bunch of steps which I need to use many times, and I would prefer not to have to copy/paste the same code over and over again.
You can not declare function in if-statements.
you could do this:
<?php
function some_function_name() {
// do something here
}
if( a == b && c == d ) {
some_function_name();
}
?>
I would avoid defining the function inside a conditional if() statement. Define the function first, at the top of the file and call it when necessary.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With