Fatal error: Cannot redeclare function
This errors says your function is already defined ; which can mean :
- you have the same function defined in two files
- or you have the same function defined in two places in the same file
- or the file in which your function is defined is included two times (so, it seems the function is defined two times)
To help with the third point, a solution would be to use include_once
instead of include
when including your functions.php
file -- so it cannot be included more than once.
Cannot redeclare function php
You (or Joomla) is likely including this file multiple times. Enclose your function in a conditional block:
if (!function_exists('parseDate')) {
// ... proceed to declare your function
}
fatal php error: `cannot redeclare function` while using include_once
include_once
ensures that file is included exactly once. It doesn't check the contents of the file or the functions in it. So when two file with same function name is added, its quite natural for the error to arise!
From the manual:
include_once
may be used in cases where the same file might be
included and evaluated more than once during a particular execution of
a script, so in this case it may help avoid problems such as function
redefinitions, variable value reassignments, etc.
italicized means that the function in the same file, not in different files.
Cannot redeclare function error in for each
I ended up figuring it out. For future devs it was easier to remove the functions all together and just do this
$team_members = get_posts(
[
'post_type' => 'team-member',
'post_status' => 'publish',
'numberposts' => -1,
]
);
?>
<div class="orbit">
<div class="orbit-wrapper">
<div class="orbit-container" style="height: 500px;">
<?php
foreach ( $team_members as $member ) {
$title = $member->post_title;
$id = $member->ID;
$positon = $member->post_content;
$post_thumbnail = has_post_thumbnail( $id );
if ( true === $post_thumbnail ) {
$image = get_the_post_thumbnail_url( $id );
} else {
$image = get_stylesheet_directory_uri() . '/dist/assets/images/avatar.png';
}
$first = explode( ' ', $title, 2 );
$first_name = $first[0];
$position_title = get_field( 'person_title', $id );
$job_summary = get_field( 'job_summary', $id );
echo
"<li class='orbit-slide'>
<figure class='orbit-figure'>
<div class='top'>
<h4>Meet $first_name</h4>
<p>$job_summary</p>
</div>
<div class='bottom'>
<img src=$image>
<h5>$title</h5>
<p>$position_title</p>
</div>
</figure>
</li>";
}
?>
<button class='orbit-previous'></button>
<button class='orbit-next'></button>
</div>
</div>
PHP Cannot redeclare function/Call to undefined function
It turns out that, unlike a standard function definition in PHP, function definitions wrapped in if (!function_exists('function_name')) { ... }
block must precede any call to that function.
In this case, the function definition (line 36) followed the call (line 32). This caused the function to appear to PHP as undefined:
// Not working!
$value = get_cc($code);
if (!function_exists('get_cc')) {
function get_cc( $code ) {
...
}
}
Switching the order so that the function definition came first fixed the issue:
// Working!
if (!function_exists('get_cc')) {
function get_cc( $code ) {
...
}
}
$value = get_cc($code);
As of right now, PHP's documentation does not mention this issue.
Laravel 8 Custom Helper function PHP Fatal error: Cannot redeclare functionName() previously declared in C:(patth)Helpers.php
You can bypass this error by checking if your function already exists:
if(! function_exists('CheckInvalidPlan')) {
function CheckInvalidPlan($id)
{
if (Plan::find($id) == null)
{
return true;
}
}
}
That's how Laravel helpers are declared:
if (! function_exists('today')) {
/**
* Create a new Carbon instance for the current date.
*
* @param \DateTimeZone|string|null $tz
* @return \Illuminate\Support\Carbon
*/
function today($tz = null)
{
return Date::today($tz);
}
}
However, a cleaner approach would be to understand why your helpers file is loaded twice.
It is hard to tell you exacly where the error could be, however you should inspect all your classes, the app\Helpers.php
file should never be required manually. It should be autoloaded by composer, as explained in this answer (thanks N69S).
PHP Cannot redeclare function, previously defined?
As per your comment, you are placing the function_exists
check in the wrong place, change your code to the following:
if (!function_exists('checkIfAdminExists'))
{
function checkIfAdminExists($username, $password) {
require_once("db_connection.php");
$sql = "SELECT personid, username, password FROM table_test";
$result = $dbcon->query($sql);
if ($result->num_rows > 0) {
while($row = $result->fetch_assoc()) {
if ($row["username"] == $username && $row["password"] == $password) {
return true;
}
}
} else {
return false;
}
$dbcon->close();
}
}
Now, with the above code, the function will only be defined if it doesn't already exist.
Note: Please stop using mysql_*
as it has been officially deprecated and removed in PHP 7. It would be wise to start learning mysqli_*
or PDO and to make use of prepared statements.
Update #1
You are setting the username variable twice:
$username = mysql_real_escape_string($username);
$username = mysql_real_escape_string($password);
It should be:
$username = mysql_real_escape_string($username);
$password = mysql_real_escape_string($password);
Update #2
As per the documentation of mysql_real_escape_string
, it takes an optional second parameter:
The MySQL connection. If the link identifier is not specified, the last link opened by mysql_connect() is assumed. If no such link is found, it will try to create one as if mysql_connect() had been called with no arguments. If no connection is found or established, an E_WARNING level error is generated.
With the above in mind, it means that your connection has not been established. So the next logical question is: Where is your connection being established in create_admin.php
?
Related Topics
Get Start and End Days for a Given Week in PHP
How to Make Apache Serve Index.PHP Instead of Index.Html
Reaching 100% Code Coverage with PHPunit
Using Array_Intersect on a Multi-Dimensional Array
What Is the Postman-Token Header Attribute in Generated Code from Postman
Create New Product Attribute Programmatically in Woocommerce
Using If(!Empty) with Multiple Variables Not in an Array
Select Within 20 Kilometers Based on Latitude/Longitude
How to Access Discriminator Field from PHP in Doctrine2
Return Errors from PHP Run Via. Ajax
Try Catch Cannot Work with Require_Once in PHP
How Many Days Until X-Y-Z Date
$Path Environment Variable for Apache2 on MAC
Replace Deprecated Preg_Replace /E with Preg_Replace_Callback
Warning: MySQLi_Real_Escape_String() Expects Exactly 2 Parameters, 1 Given... What I Do Wrong