Writing a function in php

I'm trying to figure out how to write php functions, so I donโ€™t need to write a block of code all the time, I have a game and tried to write it, but it doesnโ€™t work for some reason, can someone tell me where I am going wrong?

<?php $greeting 'Hello'; function frank () { $name = 'frank'; $2nd = 'robson'; echo $greeting; echo $name; echo $2nd; } echo frank() ?> 
0
function php echo
Jan 09 2018-12-12T00: 00Z
source share
6 answers

Instead of repeating inside your function, you should consider returning a string to it. Also prefer to pass the parameter instead of using the global scope:

 $greeting = 'Hello'; function frank ($funcGreetings) { $name = 'frank'; $second = 'robson'; //Concat variable together an return them return $funcGreetings.' '.$name.' '.$second; } echo frank($greeting); 

Finally, the variable name begins with a letter or underscore , so $2nd not a valid name.
See this link for more information about a named variable.

+3
Jan 09 2018-12-12T00:
source share

where am i wrong

A few places.

but for some reason it doesnโ€™t work

This is not an error message - PHP will provide you with meaningful messages about things that it does not understand if you configured it correctly.

If you get an error message, you should include it in your question.

 <?php $greeting 'Hello'; 

Here's the first error - this will cause the parsing to fail. There should be an assignment operator, for example

 $greeting = 'Hello'; 

The following error:

 $2nd = 'robson'; 

Variable names must begin with an underscore or letter - not a number

 echo frank() 

2 errors here.

A statement does not end with a; - he can still parse OK, but very dirty.

Also, the frank function does not return a value for the echo.

+2
Jan 09 2018-12-01T00:
source share

First, you need to pass the $ greeting variable so that it is in the function area - see the PHP manual for the area variable . It is better to avoid using global , as they are more difficult to debug and test.

Secondly, you missed the assignment for the $greeting variable.

Thirdly, you forgot to return the value from the function so that it could be echo d out. See the PHP manual for functions .

Fourth, $2nd not a valid variable name. Variables must begin with a letter. See the corresponding manual page .

 $greeting = 'Hello'; function frank ($greeting) { $return = ''; $name = 'frank'; $second = 'robson'; $return .= $greeting; $return .= $name; $return .= $second; return $return; } echo frank($greeting); 
+1
Jan 09 2018-12-01T00:
source share

I see you want a function that will return something string. You can pass a variable as an argument. Things after return will do what you get by calling frank()

 $greeting = 'Hello'; function frank ( $greeting) { $name = 'frank'; $second = 'robson'; return $greeting." ".$name." ".$second; } echo frank ( $greeting ); 

Or, if you want to use a global variable in a function, you must declare it with the global operator

 $greeting = 'Hello'; function frank ( $greeting) { global $greeting; $name = 'frank'; $second = 'robson'; return $greeting." ".$name." ".$second; // } echo frank ( $greeting ); 
+1
Jan 09 2018-12-01T00:
source share

You should return a string when using your function with echo

So do something like this

 //this is inside frank $ret = $greeting.$name.$2nd; return $ret; 

Secondly, $greeting is a variable declared outside the function definition. Therefore, you should not use it inside a function definition.

Now you will look like this:

 function frank () { $greeting = 'hello'; $name = 'frank'; $2nd = 'robson'; $ret = $greeting.$name.$2nd; return $ret; } 

In addition, there is an error in the first line. $greeting 'Hello' is incorrect. It should be $greeting = 'Hello' (you don't have the = sign).

+1
Jan 09 '12 at 9:50
source share
 <?php $greeting = 'Hello'; function frank () { global $greeting; $name = 'frank'; $2nd = 'robson'; echo $greeting; echo $name; echo $2nd; } frank(); ?> 

You need to set a variable and you will need SEMICOLONS.

0
Jan 09 2018-12-01T00:
source share



All Articles