Jump to content

[SOLVED] I fail at making functions


bloodgoat

Recommended Posts

Trying to make a function to create a file if it doesn't exist with a specified filename and specified array name in the function parameters.

 

Here's my function as it is:

<?php
function create($file,$array){
$new = "<?php \$array = array(); ?>";
$h = fopen($file, "w+");
fwrite($h, $new);
fclose($h);
header("Location: ".$index);
}
?>

And when I call my function:

<?php
if(!file_exists($chat)){
create($chat, $post);
}
?>

 

But when I run it, it just creates the file with:

<?php $array = array(); ?>

 

Instead of (what I want):

<?php $post = array(); ?>

Link to comment
https://forums.phpfreaks.com/topic/156294-solved-i-fail-at-making-functions/
Share on other sites

And when I call my function:

<?php
if(!file_exists($chat)){
create($chat, $post);
}
?>

 

But when I run it, it just creates the file with:

<?php $array = array(); ?>

 

Instead of (what I want):

<?php $post = array(); ?>

You cannot retrieve the names of variables that have been passed to a function, only their values. So if you want to name your array $post when the file is created you'll have to call your function like so

create($chat, 'post');

 

Now define $new in your function as

$new = '<?php $'.$array.' = array(); ?>';

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.