0
votes

Newb here.

I'm trying to create my first php function which will get the name of the file, stripe out the '.php' extension and return a string which is the name of the page file without the '.php' file. This if i was using the index page, the title tag would read 'Index'.

To create the function, first i wrote the following process which did what I hoped:

$fileName2 = basename($_SERVER['PHP_SELF']);
$pageName2 = str_replace(".php","", $fileName2);

However when i try to put it into the context/format of a function i get an error stating:

NOTICE: UNDEFINED VARIABLE: ITC240.PHP ON LINE 14

Here is what i believe to be the relevant code:

inc_function.php:

function GetPageName($pageName) {
$fileName = basename($_SERVER['PHP_SELF']);
$pageName = str_replace('.php','', $fileName);
}

I call the function between the title tags:

<?php echo GetPageName(); ?>

(I tried to put this in code block but every time i put anything in the title tags it didn't print for reasons i don't understand).

Any help or instruction is very much appreciated

2
I don't know why, but part of my code isn't showing - trying to resolve this - Chezshire
I just edited your question because of your missing indents and you overwrote it. I'm not re-editing. - Funk Forty Niner
I've indented it several times - i can't seem to get it all to format - i am on lots of nyQuil though so i'm sure the fault here is all mine. Sorry - i had no way of know you were trying to edit it or were editing it. Sorry - Chezshire
Add a return before the code to create a code block. - rrrfusco
Thank you ... Daryl Gill's comment totally confuses me and has nothing to do with this right? I'm a real newb (on nyquil no less) so i'm just a twit now i guess. Thank you I will try again. - Chezshire

2 Answers

2
votes

Restructured Answer

You are being shown an error because your function is looking for data & using the information from your comment I don't know why but stack overflow kills anything i plop inside title tags: This is what is in the title tags: <?php echo GetPageName(); ?>

So using my best judgement from information provided, I'm assuming you want to have a function to automatically return something with no data specification. In this case, you would need to take note of 2 things

  1. The return block in your function will essentially, do as the function says. Return something so it can be used
  2. No requirements in your function definition braces

    function TestFunc($RequestedData){ }

the above function definition is asking the developer for information to be provided to the function for data manipulation.

function TestFunc(){  }

The above function definition is asking for nothing from the developer apart from calling, doing this will enable you to perform exactly what you want.

So, to fix your function:

function GetPageName() {
    $fileName = basename($_SERVER['PHP_SELF']);
    $pageName = str_replace('.php','', $fileName);
    return $pageName;
}

var_dumps to show the power of return

Using the exact code above on my local development machine Web Addr of (http://127.0.0.1) returns:

string(5) "index"

Now, using the exact function as specified above, just without the return:

NULL

1
votes

to get only the name of the file use :

$file = basename($path);         // $file is set to "index.php"
$file = basename($path, ".php"); // $file is set to "index"