Jump to content

An extremely simple problem with functions.


Hangwire

Recommended Posts

Hello all, I finally started learning php and with the little knowledge I have from CPP it's far simpler than it seemed years ago. I need a little bit of help with this function I followed in a tutorial and it's just not working.

 

<?php
function writeName($fname)
{
echo $fname . " Refsnes.<br />";
}

echo "My name is ";
writeName("Kai Jim");
echo "My sister's name is ";
writeName("Hege");
echo "My brother's name is ";
writeName("Stale");
?>

 

It just gives me a blank screen. Now, I figured #fname isnt set, so I did this:

 

<?php
$fname = "Refsnes";
function writeName($fname)
{
echo $fname . " Refsnes.<br />";
}

echo "My name is ";
writeName("Kai Jim");
echo "My sister's name is ";
writeName("Hege");
echo "My brother's name is ";
writeName("Stale");
?>

 

Still, no go, which leads me to think I'm probably mistaking.

 

Help? I don't want to skip out things in a tutorial.  ;)

Link to comment
Share on other sites

That code works for me no problem. My guess is that the problem is elsewhere; possibly the web server. Try the code below in a file of it's own:

 

<?php phpinfo(); ?>

 

If you still see a blank screen you don't have the server configured correctly.

Link to comment
Share on other sites

Hi

 

I just tried your code and it worked fine.

 

$fname is a parameter to the function so is set by the call to the function. You do not need to give it a value at the start. Indeed inside the function $fname is referring to the parameter and the one you have defined specifically is a separate variable (if you want to access a variable that is outside the function from within the function you either need to pass it as a parameter or use the global statement to specify that you want to refer to it).

 

All the best

 

Keith

Link to comment
Share on other sites

Make sure the code is within a .php file. Also make sure you are running the PHP script from a server environment, either locally (eg http://localhost) or on your website. Passing the php file directly to your browser will not work as web browsers do not understand PHP code.

 

You can install amp packages such wampserver (windows only) or xampp (windows, mac, linux) to run your PHP code locally on your PC.

Link to comment
Share on other sites

Thank you for all the replies, then I'm afraid something is wrong with how i set up my webserver. I'm using apache and PHP 5, so far I've had no problems with any of the functions that I've had from that tutorial (tried them, no problem).

Also, I execute all of them under firefox as php.php (it opens them without any problem)

This is what I've done so far.

 

    <?php
    $cars[0]="Saab";
    $cars[1]="Volvo";
    $cars[2]="BMW";
    $cars[3]="Toyota"; 

    echo $cars[0] . " and " . $cars[1] . " are Swedish cars." . $cars[2] . " and " . $cars[3] . " are not."
    ?>

<br />

        <?php
    $ages['Peter'] = "32";
    $ages['Quagmire'] = "30";
    $ages['Joe'] = "34";

    echo "Peter is " . $ages['Peter'] . " years old.";
    ?> 
    
<br />

        <?php
        $families = array
      (
      "Griffins"=>array
      (
      "Peter",
      "Lois",
      "Megan"
      ),
      "Quagmires"=>array
      (
      "Glenn"
      ),
      "Browns"=>array
      (
      "Cleveland",
      "Loretta",
      "Junior"
      )
      ); 
      
      echo "Is " . $families['Griffins'][2] . " a part of the Griffin family?" . 
      "Is " . $families['Quagmires'][0] . " a pervert?";
      ?>
      
      <br />
      
    <?php
    $i=1;
    while($i<=5)
      {
      echo "Chisloto e " . $i . "<br />";
      $i++;
      }
    ?>
    
    <br />
    
    <?php
    $i=1;
    do
      {
      $i++;
      echo "The number is " . $i . "<br />";
      }
    while ($i<=5);
    ?>
    
    <br />
    
    <?php
    $i=1;
    do
      {
      $i++;
      echo "The number is " . $i . "<br />";
      }
    while ($i<=5);
    ?>
    
    <br />
    
    <?php
    for ($i=1; $i<6; $i++)
      {
      echo "The number is " . $i . "<br />";
      }
    ?>
    
    <br />
    
    <?php
    $g=array("edno","dve","tri");
    foreach ($g as $value)
      {
      echo $value . "<br />";
      }
    ?>
    
    <br />
    
    <?php
    function writeName()
    {
    echo "Smurt Ivanova";
    }
    echo "My name is ";

    writeName();
    ?>
    
    <br />
    

 

On and On and On and before that - all simple tutorial ones. I use that code just below all others and it gives me a blank page. When I remove it, all the above display normal results.

Help?

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.