PHP function getHeader() - php

Hey guys, Im building a fairly large website here, I am using quite a bit of php along with it, but what I was wondering, I have a header that does't change throughout the website, and I was wondering if I could create a function in some of my php code where all I would have to do is call like a function getHeader() and it will return the header. Now this header has some php in it also like a search bar and a username container... I was just wondering if this was possible on a fairly simple scale so I don't have to place the header code in each php file. which is fine but if I happen to make an update I have to update file which could take some time...
Thanks in advance!

Just create a header file (e.g. header.php) and include it.
http://php.net/manual/en/function.include.php
You can either have the code directly in the header.php not in any functions, and it will run by default, or put it in a function and call it manually.

A simple include() at the top of every file is what you'll need http://php.net/manual/en/function.include.php

You could use a function. That's what WordPress does. You could even put PHP inside them. You'd just have to keep in mind that the PHP inside your function is... well... inside a function. So you'd have to explicitly access global variables, etc.
Another option would be to put the header in its own, single file and just include that file, instead of calling a function. Whatever floats your boat.

Related

If file.html changed to file.php how to keep the jQuery working?

I had to change a file suffix from .html to .php so a PHP include works, but after doing that the jQuery does not work any more. Is that normal? Is there any way to keep jQuery working?
No, it is not normal. I don't see your code, but I have 2 assumption:
How do you call your php file? Do you call it same way as html? If you call html from local path (c:/dir1/dir2/file.htlm) and php from server (http://localhost/file.php) then possible reason is path to jQuery
What is php include? Try to remove PHP code and try again. (with PHP extension)
ps. Do you get any jQuery-related errors in your browser?
You have to handle it like HTML. It should stand outside the PHP Tags or you have to use echo or something similar.
Thanks Andray. It is working now. I must have been doing something wrong, or perhaps was not refreshing the code properly into my testing site. Yes, the php behaves very much as the original .html including the java scripts in it.

Executing only the code of a page?

Hi guys consider I have a .php page that I need to include somewhere else.
page.php:
<?php
dosomecmd();
doothercmd();
//etc
?><html>pagehtml</html>
Is there a way to include this page but not printing the html inside? (I just need to execute those command)
I could do with ob
ob_start();
include('page.php');
ob_discard();
But i would prefer a faster way.
Thanks
Edit: i know i can sepearate html and php (and i already do) but that page.php is non else than a "static" cache I make, but sometimes I need to execute some command inside that cache instead to printing automatically it out
Edit2: of course i don't need everyime to not output the html (otherwise I could just delete all html) I need to return; only based on the results of my cmds up there
Thanks all i find a solution (add return;)
Would it be easier to have a common page with only the php that you need to share, and include it in both page.php and your other page?
you can try to seperate with
$_SERVER['REQUEST_URI']
also I wouldn't recommend that
Try to separate the PHP logic at the start of your current script into another file, then include that into both of your scripts.

Variable scope and includes

I have the following problem: in index.php, I have set the variable $activelang. I
$activelang = active_language ();
echo $activelang; //works perfectly
Later in my index.php code, I include a new php file.
include ('myotherfile.php');
If I try to use $activelang in myotherfile.php, it does not work! I am not using it inside a function, and I tried using global $activelang and it doesnt work either
All of these is happening in a wordpress install, but the code I am talking about is plain php. I am using php 5.3
Why is this happening? As I undestand it, the include works as a copy paste in my main file, so I shouldnt be having any problems with variable scope, right?
include should be used to include files, not some network resources.
if you bother to run the code you posted here you'll be surprized, as it will print $activelang all right.
When working with WordPress templates you should create functions in your functions.php file for handling includes and then call those functions in your templates.
So in functions.php you should have:
function myOtherFile() {
include('myotherfile.php');
}
And in index.php call myOtherFile()
Now, if you still have trouble with scope try this:
function myOtherFile($activelang) {
include('myotherfile.php);
}
And then in index.php do this:
$activelang = active_language();
myOtherFile($activelang);
If that doesn't work then I think your problem is being caused elsewhere. Because like Col. Shrapnel said, the code you posted here works.

Using a query-string in require_once in PHP

On one of my pages I have a require_once('../path/to/url/page.php'); which works with no problems. The moment I add a query string require_once('../path/to/url/page.php?var=test'); it won't include the file anymore. It's just blank. Anyone have any ideas of why? Can you not use a query-string in a require?
Thanks,
Ryan
By using require_once('../path/to/url/page.php?var=test');, php will not make a new request to page.php, it will actually search for the file named page.php?var=test and include it, because in unix, you are allowed to have such a filename. If you want to pass a variable to that script, just define it: $var="test" and it will be available for use in that script.
require loads a File (from a file path) to include. It does not request that file through apache (or other webserver), therefore you cannot pass query strings in this way.
If you need to pass data into the file, you can simply define a standard php variable.
Example
<?php $a_variable = "data"; require_once('../path/to/url/page.php'); ?>
Note, the variable must be set before the include/require is called, otherwise it won't be available.
All answes true. But most importantly: since $_GET is a global, it's present' in all included files as well, so there's absolutely no use in passing those parameters with the include.
require only accepts paths it would be pointless to add any request since it doesn't make any
it simple includes the required code into the current one

Storing the HTML output from a local PHP file into a string using file_get_contents

There is a header.php file and it contains some php codes that return HTML.
I know I can use require, include to echo the results, but what I want to do is to store its processed output string into a variable.
In a page, I used:
$headerHTML=file_get_contents('header.php');
Then I got the PHP code output rather than the processed HTML output.
I know adding http:// would help.
But I prefer to keep using relative path, how can I tell the function to treat the php file correctly?
Note: I would like to continue to use this statement file_get_contents rather than using ob_start() if possible.
I'd rather use require() wrapped inside ob_start() and ob_get_clean(). I am sure there is nothing wrong with this approach.
Don't use eval() - it's evil!
Use the relative local path an automatically map it to a absolute URL.
If URL wrappers are enabled and you want the output of header.php (and you don't want to keep session state) you could use $headerHTML=file_get_contents('http://yourdomain.tld/path/to/header.php');, though why you would want to do such a thing eludes me. Are you sure you're not trying to do something that could easily be solved by using templates and caching?
You can check http://in2.php.net/manual/en/function.eval.php#56641, hope it helps.

Categories