Solved

How do I write this PHP syntax/function?

Posted on 2009-04-01
7
257 Views
Last Modified: 2012-05-06
I have a php function that I have broken down to simplify my question.

Basically, I want the function to insert php code to wherever it's called.

NB.  I don't want it to display code to the viewer, just to the server.

So if I call fake()...it would be as if I had coded:
<span class='sv'><?php echo $message ?></span>


The variable $message is declared earlier on the page..  When fake() is called...it will echo the value of $message inside the span...and the viewer will be able to see what $message says (formatted according to class sv).

I know this question seems silly, but I have simplified my actual code so that I the question can be more easily answered.

Thanks in advance!
Regards
Nick
0
Comment
Question by:Chimeraza
7 Comments
 
LVL 9

Assisted Solution

by:Mahdii7
Mahdii7 earned 150 total points
ID: 24044504
Is this what you're looking for?
<?php

function fake($message){

	$return = "<span class='sv'>" . $message . "</span>";

	return $return;

}

?>

<?php echo fake("test");  // Outputs: <span class='sv'>test</span> ?>

Open in new window

0
 
LVL 108

Assisted Solution

by:Ray Paseur
Ray Paseur earned 150 total points
ID: 24044520
Set PHP Short Tags on.  This should do it for you.
<?php // RAY_temp_fake.php

function fake($message)

{

   echo "<span class='sv'>$message</span>\n";

}

?>

<!-- CALLING SEQUENCE -->

<br/><?=fake("MY TEXT")?>

Open in new window

0
 
LVL 39

Accepted Solution

by:
Roger Baklund earned 200 total points
ID: 24044551
You said the $message variable was declared earlier on the page. Then this can be used:
function fake() {

  global $message;

  return '<span class="sv">'.$message.'</span>';

}
 

# Usage:

<?php echo fake(); ?>

Open in new window

0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 3

Author Comment

by:Chimeraza
ID: 24044582
thanks for the quick responses...

I was being stupid...  I thought that $message wouldn't be an available variable... can't believe I didn't think of passing it to the function..  I was trying to access the variable remotely..

@Ray:  What do short tags do?
0
 
LVL 108

Expert Comment

by:Ray Paseur
ID: 24044591
They let you say <?=$var?> in your HTML.  Instead of <?php echo $var; ?>

Just saves typing ;-)
0
 
LVL 3

Author Closing Comment

by:Chimeraza
ID: 31565564
technically.. CXR was the most correct...global is good to use in my case...but all will work...
0
 
LVL 3

Author Comment

by:Chimeraza
ID: 24044604
Thanks...at least I learned something!
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
How to use md5 hashing 3 31
Php pie charts 3 26
WordPress syntax 2 28
PHP get array item with custom id 4 25
I imagine that there are some, like me, who require a way of getting currency exchange rates for implementation in web project from time to time, so I thought I would share a solution that I have developed for this purpose. It turns out that Yaho…
Things That Drive Us Nuts Have you noticed the use of the reCaptcha feature at EE and other web sites?  It wants you to read and retype something that looks like this.Insanity!  It's not EE's fault - that's just the way reCaptcha works.  But it is …
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

863 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

23 Experts available now in Live!

Get 1:1 Help Now