bloodgoat Posted April 30, 2009 Share Posted April 30, 2009 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 More sharing options...
Ken2k7 Posted April 30, 2009 Share Posted April 30, 2009 <?php function create($file,$array){ $new = '<?php $array = array(); ?>'; $h = fopen($file, "w+"); fwrite($h, $new); fclose($h); header("Location: ".$index); } ?> Link to comment https://forums.phpfreaks.com/topic/156294-solved-i-fail-at-making-functions/#findComment-822837 Share on other sites More sharing options...
wildteen88 Posted April 30, 2009 Share Posted April 30, 2009 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(); ?>'; Link to comment https://forums.phpfreaks.com/topic/156294-solved-i-fail-at-making-functions/#findComment-822846 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.