Include_once

Ask about general coding issues or problems here.

Moderators: egami, macek, gesf

Post Reply
User avatar
DanMan3395
New php-forum User
New php-forum User
Posts: 14
Joined: Tue Feb 13, 2018 12:39 pm
Location: Minneapolis, MN

Mon Feb 19, 2018 11:56 am

I am trying to learn how to pull a function off of a different .php file and use it to process data on the site i pulled data into. I am quite new to coding PHP so i don't know all the tricks yet. I ASSUME there is like a repository of functions that people keep and use and this process is pretty common so I figured i would ask if anyone has an easy example they could post i could then use to test and pick apart.

Essentially I am on PAGE1.php and have a variable or a result from an expression, i want to run that variable or expression through a function that i used by linking it to PAGE1.php from PAGE2.php. Likely using include_once. Anyone got an easy explanation of this?

Example, I have 2 pages:

Page1.:

Code: Select all

 <!DOCTYPE html>
<html>
<body>

<h1>test SITE!<h1>
<p>I made this store</p>
<p>Below is a list of crap you can buy</p>
<hr>
<?php include 'include.php';?>

</body>
</html>
Page 2.:

Code: Select all

$crap = array('Hoses' => "Assorted lengths of used hose",
                 'Poop' => "Collection of dog poop going back years from various dogs",
                 'Urin'  => "My Lifetime collection of urin in jars",
                 'VHS'  => "My collection of vhs home movies");

  foreach($crap as $item => $description)
    echo "$item: $description<br>";
Is there a more standard and possibly more advanced version of something like this i could tool around with?
Dan

User avatar
benanamen
New php-forum User
New php-forum User
Posts: 44
Joined: Mon Oct 16, 2017 1:06 pm

Mon Feb 19, 2018 1:55 pm

You are on the right track with the include. If you are not going to use the page 2 code more than once there is no need to create a function for it. Just put it in page 1.

If you will be using the page 2 code more than once, turn it into a function and put it in a file that you will store all your other functions (functions.php) and then include that file.

If you develop with a single page entry to the site/app you only need to include the functions file once in the main page.
The XY Problem
The XY problem is asking about your attempted solution (X) rather than your actual problem (Y). This leads to enormous amounts of wasted time and energy, both on the part of people asking for help, and on the part of those providing help.

User avatar
DanMan3395
New php-forum User
New php-forum User
Posts: 14
Joined: Tue Feb 13, 2018 12:39 pm
Location: Minneapolis, MN

Mon Feb 19, 2018 2:10 pm

benanamen wrote:
Mon Feb 19, 2018 1:55 pm
You are on the right track with the include. If you are not going to use the page 2 code more than once there is no need to create a function for it. Just put it in page 1.

If you will be using the page 2 code more than once, turn it into a function and put it in a file that you will store all your other functions (functions.php) and then include that file.

If you develop with a single page entry to the site/app you only need to include the functions file once in the main page.
Yeah that's what i am trying to learn. How would i turn this into a function so i can add it to a reference page for use on multiple pages. Sorry I am VERY new to this.
Dan

User avatar
DanMan3395
New php-forum User
New php-forum User
Posts: 14
Joined: Tue Feb 13, 2018 12:39 pm
Location: Minneapolis, MN

Mon Feb 19, 2018 2:25 pm

Code: Select all

 function Task() {
    $crap = array('Hoses' => "Assorted lengths of used hose",
                 'Poop' => "Collection of dog poop going back years from various dogs",
                 'Urin'  => "My Lifetime collection of urin in jars",
                 'VHS'  => "My collection of vhs home movies");

  foreach($crap as $stuff => $desc)
    echo "$stuff: $desc<br>";
	echo "<hr>";
}
I think this did it.
Dan

User avatar
benanamen
New php-forum User
New php-forum User
Posts: 44
Joined: Mon Oct 16, 2017 1:06 pm

Mon Feb 19, 2018 7:47 pm

return the array. Don't format in the function.

Code: Select all

<?php
function Task() {
  $crap = [
    'Hoses' => "Assorted lengths of used hose"
  , 'Poop' => "Collection of dog poop going back years from various dogs"
  , 'Urin' => "My Lifetime collection of urin in jars"
  , 'VHS' => "My collection of vhs home movies"
  ];
  return $crap;
}

$array = Task();
foreach ($array as $stuff => $desc) {
  echo "$stuff: $desc<br>";
  echo "<hr>";
}
?>
The XY Problem
The XY problem is asking about your attempted solution (X) rather than your actual problem (Y). This leads to enormous amounts of wasted time and energy, both on the part of people asking for help, and on the part of those providing help.

Post Reply