473,406 Members | 2,710 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

How to keep global strings in another file?

Hi!

I want to have a number of strings in another file, which I can
include everywhere... but I cannot make them reachable from the top
file. I have looked at var and global to to this but without luck.
The idea is to have all text the same place, which minimses changes
and spelling errors.

something like error messages stored:

<?
$sError001="stupid user";
$sError002="not available";
?>

and then :

include "errormsgs.php";
echo $sError001;

BR
Sonnich

Mar 15 '07 #1
9 1692
Message-ID: <11**********************@b75g2000hsg.googlegroups .comfrom
Sonnich contained the following:
>something like error messages stored:

<?
$sError001="stupid user";
$sError002="not available";
?>

and then :

include "errormsgs.php";
echo $sError001;
Well yeah, that will work, so what is the question?

Though personally, I'd use an array.

<?
$sError[1]="stupid user";
$sError[2]="not available";

?>
>
and then :
include "errormsgs.php";
echo $sError[1];
--
Geoff Berrow (put thecat out to email)
It's only Usenet, no one dies.
My opinions, not the committee's, mine.
Simple RFDs http://www.ckdog.co.uk/rfdmaker/
Mar 15 '07 #2
Sonnich schreef:
Hi!

I want to have a number of strings in another file, which I can
include everywhere... but I cannot make them reachable from the top
file. I have looked at var and global to to this but without luck.
The idea is to have all text the same place, which minimses changes
and spelling errors.

something like error messages stored:

<?
$sError001="stupid user";
$sError002="not available";
?>

and then :

include "errormsgs.php";
echo $sError001;
This should work. You have another error. \

Btw set it up a little different. That way you can allways change ur
error code without redoing the same echo over and over again.

include('somefile');
showerror(1);

function showerror($no)
{
$errors=array(
1=>'wrong page',
2=>'hack attempt'
)

echo $errors[$no];
// do stuff here later
}

--
Arjen
http://www.hondenpage.com
Mar 15 '07 #3
On Mar 15, 3:20 pm, Geoff Berrow <blthe...@ckdog.co.ukwrote:
Message-ID: <11**********************@b75g2000hsg.googlegroups .comfromSonnichcontained the following:
something like error messages stored:
<?
$sError001="stupid user";
$sError002="not available";
?>
and then :
include "errormsgs.php";
echo $sError001;

Well yeah, that will work, so what is the question?
The point it, that it does not work.

Mar 15 '07 #4
Sonnich schreef:
On Mar 15, 3:20 pm, Geoff Berrow <blthe...@ckdog.co.ukwrote:
>Message-ID: <11**********************@b75g2000hsg.googlegroups .comfromSonnichcontained the following:
>>something like error messages stored:
<?
$sError001="stupid user";
$sError002="not available";
?>
and then :
include "errormsgs.php";
echo $sError001;
Well yeah, that will work, so what is the question?

The point it, that it does not work.
And his point is that it does work :-) You have another error somewhere
... are you sure you have included the correct file?

try this->
errormsg.php

echo "you have included errormsg.php";

--
Arjen
http://www.hondenpage.com
Mar 15 '07 #5
On Mar 15, 3:32 pm, Floortje <l...@zingmaarmetmijmee.enelwrote:
Sonnichschreef:On Mar 15, 3:20 pm, Geoff Berrow <blthe...@ckdog.co.ukwrote:
Message-ID: <1173963662.556376.252...@b75g2000hsg.googlegroups .comfromSonnichcontained the following:
>something like error messages stored:
<?
$sError001="stupid user";
$sError002="not available";
?>
and then :
include "errormsgs.php";
echo $sError001;
Well yeah, that will work, so what is the question?
The point it, that it does not work.

And his point is that it does work :-) You have another error somewhere
.. are you sure you have included the correct file?
I found, that my problem is to include this into a class...

include...

class whatever
{
$somthing= $somthing_from_include_file.

In other words, I want to export texts used in my class to a file...

/S

Mar 15 '07 #6
Classes are somewhat different. Run this code (maybe fix a minor speling
error .. I didn't test) and compare it with your code.

class a
{
public $error1

public function a()
{
$this->error1='howdy';
}
}

class b extends a
{
private function b()
{
echo $this->error1;
}
class c
{
private function c()
{
global $a
echo $a->error1;
}
}

$a=new a
$b=new b
$c=new c

--
Arjen
http://www.hondenpage.com
Mar 15 '07 #7
Sonnich wrote:
On Mar 15, 3:32 pm, Floortje <l...@zingmaarmetmijmee.enelwrote:
>Sonnichschreef:On Mar 15, 3:20 pm, Geoff Berrow <blthe...@ckdog.co.ukwrote:
>>>Message-ID: <1173963662.556376.252...@b75g2000hsg.googlegroups .comfromSonnichcontained the following:
something like error messages stored:
<?
$sError001="stupid user";
$sError002="not available";
?>
and then :
include "errormsgs.php";
echo $sError001;
Well yeah, that will work, so what is the question?
The point it, that it does not work.
And his point is that it does work :-) You have another error somewhere
.. are you sure you have included the correct file?

I found, that my problem is to include this into a class...

include...

class whatever
{
$somthing= $somthing_from_include_file.

In other words, I want to export texts used in my class to a file...

/S
It would help if you would post your entire code - not just little pieces.

For instance, your first statement was:

"I want to have a number of strings in another file, which I can include
everywhere... but I cannot make them reachable from the top file.".

Now you're saying:

"In other words, I want to export texts used in my class to a file..."

Two entirely different things.

And BTW - you should be using <?php instead of just <?. Most hosts
nowadays are running with short_tags_off.
--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
js*******@attglobal.net
==================
Mar 15 '07 #8
On Mar 15, 5:34 pm, Jerry Stuckle <jstuck...@attglobal.netwrote:
Sonnichwrote:
On Mar 15, 3:32 pm, Floortje <l...@zingmaarmetmijmee.enelwrote:
Sonnichschreef:On Mar 15, 3:20 pm, Geoff Berrow <blthe...@ckdog.co.ukwrote:
Message-ID: <1173963662.556376.252...@b75g2000hsg.googlegroups .comfromSonnichcontained the following:
something like error messages stored:
<?
$sError001="stupid user";
$sError002="not available";
?>
and then :
include "errormsgs.php";
echo $sError001;
Well yeah, that will work, so what is the question?
The point it, that it does not work.
And his point is that it does work :-) You have another error somewhere
.. are you sure you have included the correct file?
I found, that my problem is to include this into a class...
include...
class whatever
{
$somthing= $somthing_from_include_file.
In other words, I want to export texts used in my class to a file...
/S

It would help if you would post your entire code - not just little pieces.

For instance, your first statement was:

"I want to have a number of strings in another file, which I can include
everywhere... but I cannot make them reachable from the top file.".

Now you're saying:

"In other words, I want to export texts used in my class to a file..."

Two entirely different things.
Hardly. I could also export all error messages to a file. I can take
any text and put it in a file. Appearently we have different opnions
about this.

Code as I have it:

Include file:
<?php
$test="hello";

//---------------------------------- header for sheets...
$sSheet_001="blablaabla
File, which uses the include file:

<?php
include '/functions.php'; <------------ basic functions and other
stuff, used in class.
include 'cls_inc.php'; <----------- here I include the file

class clsMyClass
{
var $Contens = array();

function SaveFile($filename)
{
// extract values for later use
$name=ExtractFilename($filename); <--------- functions from
functions.php
$path=ExtractFilePath($filename);
$i=strrpos($name, ".");
if($i===false)
$files=$name."_files";
else
$files=substr($name, 0, $i)."_files";
$filesdir=$path."\\".$files;
// create directory for
if(!file_exists($filesdir))
mkdir($filesdir);

$fFile=fopen($filesdir."\\page".Format($i, 3).".htm", "w");
fwrite($fFile, $sSheet_001); <------------ this is where the
string is used...
// save contens of page
for($y=0;$y<count($this->Contens[$i]);$y++)
{
fwrite($fFile, "<tr>...
.....

Does this explain?

BR
Sonnich

Mar 15 '07 #9
Rik
Sonnich <so************@elektrobit.comwrote:
On Mar 15, 5:34 pm, Jerry Stuckle <jstuck...@attglobal.netwrote:
>Sonnichwrote:
On Mar 15, 3:32 pm, Floortje <l...@zingmaarmetmijmee.enelwrote:
Sonnichschreef:On Mar 15, 3:20 pm, Geoff Berrow
<blthe...@ckdog.co.ukwrote:
>>>Message-ID:
<1173963662.556376.252...@b75g2000hsg.googlegroup s.com
fromSonnichcontained the following:
>>>>something like error messages stored:
<?
$sError001="stupid user";
$sError002="not available";
?>
and then :
include "errormsgs.php";
echo $sError001;
Well yeah, that will work, so what is the question?
The point it, that it does not work.
And his point is that it does work :-) You have another error
somewhere
>.. are you sure you have included the correct file?
I found, that my problem is to include this into a class...
include...
class whatever
{
$somthing= $somthing_from_include_file.
In other words, I want to export texts used in my class to a file....
/S

It would help if you would post your entire code - not just little
pieces.

For instance, your first statement was:

"I want to have a number of strings in another file, which I can include
everywhere... but I cannot make them reachable from the top file.".

Now you're saying:

"In other words, I want to export texts used in my class to a file..."

Two entirely different things.

Hardly. I could also export all error messages to a file. I can take
any text and put it in a file. Appearently we have different opnions
about this.

Code as I have it:

Include file:
<?php
$test="hello";

//---------------------------------- header for sheets...
$sSheet_001="blablaabla
File, which uses the include file:

<?php
include '/functions.php'; <------------ basic functions and other
stuff, used in class.
include 'cls_inc.php'; <----------- here I include the file

class clsMyClass
{
var $Contens = array();

function SaveFile($filename)
{
// extract values for later use
$name=ExtractFilename($filename); <--------- functions from
functions.php
$path=ExtractFilePath($filename);
$i=strrpos($name, ".");
if($i===false)
$files=$name."_files";
else
$files=substr($name, 0, $i)."_files";
$filesdir=$path."\\".$files;
// create directory for
if(!file_exists($filesdir))
mkdir($filesdir);

$fFile=fopen($filesdir."\\page".Format($i, 3).".htm", "w");
fwrite($fFile, $sSheet_001); <------------ this is where the
string is used...
// save contens of page
for($y=0;$y<count($this->Contens[$i]);$y++)
{
fwrite($fFile, "<tr>...
.....

Does this explain?
It does. You variables are not in the methods scope, so they're not
available. There are several solutions:

1. Define them as constants rather then variables. Constants can be used
everywhere.
2. Include the file in the method itself.
3. Pass the variables in the function/method call.
4. Use the global keyword.
5. Use the $GLOBALS array.
6. Set them as class variables in your class declaration or constructor.

4 & 5 are very ugly, which one to use highly depends on how the entier
code is supposed to work, wether it should be repeatable, etc.

Normally I'd opt for 3 when the the variables are also used elsewhere, and
6 if they're only relevant to this class.
--
Rik Wasmus
Posted on Usenet, not any forum you might see this in.
Ask Smart Questions: http://tinyurl.com/anel
Mar 15 '07 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: mark4asp | last post by:
What are the best methods for using global constants and variables? I've noticed that many people put all global constants in a file and include that file on every page. This is the best way of...
59
by: seberino | last post by:
I've heard 2 people complain that word 'global' is confusing. Perhaps 'modulescope' or 'module' would be better? Am I the first peope to have thought of this and suggested it? Is this a...
2
by: Estella | last post by:
Hello, I wrote a function called eat_path() to split a string into components e.g. /a/b/c ==> namePtr = a,namePtr = a, namePtr = c // Global variable char *namePtr = {0}; int n; /*number of...
6
by: martin | last post by:
Hi, I have noticed that every aspx page that I created (and ascx file) has an assosiated resource file aspx.resx. However what I would like to do is have a single global resource file for the...
6
by: calcop | last post by:
Hello everyone, I hope I can write this question clearly. Anyway, it is about global variables. I have several files in my C++ project. One of which is main.cpp. This file comtains my main()...
8
by: ben | last post by:
hello there, oh dear, oh dear. here's a non global array of strings: char *chararray = { "abc", "defgh", "ijklmop" }; how do i do that so chararray is global? what goes in a .h file and...
3
by: Gediminas | last post by:
Hello, We made a project which is using global resources strings for buttons (ASP.NET 2.0). On development workstation (Windows XP, VS 2005 with .NET 1.1 & 2.0) everything was fine, but when we...
7
by: mail747097 | last post by:
I would like to keep IIS alive on my web site and prevent Application_End from occuring in global.asax. Any ideas?
112
by: istillshine | last post by:
When I control if I print messages, I usually use a global variable "int silent". When I set "-silent" flag in my command line parameters, I set silent = 1 in my main.c. I have many functions...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.