PHP embedding functions inside strings?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting PHP embedding functions inside strings?
# 1  
Old 08-28-2012
PHP embedding functions inside strings?

How to do a simple shell command like:

Code:
echo "Today's date is `date +%D`"

with PHP? Basically I'm looking to embed PHP library functions calls inside of PHP strings in much the same manner as above. Thanks
# 2  
Old 08-29-2012
Anybody?

Moderator's Comments:
Mod Comment
Bumping up posts or double posting is not permitted in these forums.

Please read the rules, which you agreed to when you registered, if you have not already done so.

You may receive an infraction for this. If so, don't worry, just try to follow the rules more carefully. The infraction will expire in the near future

Thank You.

The UNIX and Linux Forums.
# 3  
Old 08-29-2012
PHP Code:
<?php
$date 
date();
echo 
"Today's date is $date";
?>

Note the double quotes. PHP will evaluate the variable in strings with double quotes (not single quotes).
This User Gave Thanks to Neo For This Post:
# 4  
Old 08-29-2012
Using concatenation:
Code:
<? echo "Today's date is " . date("Y-m-d") . "\n"; ?>

# 5  
Old 08-29-2012
Quote:
Originally Posted by neutronscott
Using concatenation:
Code:
<? echo "Today's date is " . date("Y-m-d") . "\n"; ?>

The above (neurtrongscott's example) is how I would do it in practice....

I don't like the style of putting PHP $vars in inside strings surrounded by double quotes. It can get confusing and hard to debug in code.
# 6  
Old 08-29-2012
What if you're reading in a large file using file_get_contents() and you want the variables in that file to be evaluated? This file would not contain PHP code.
# 7  
Old 08-29-2012
Hmmmm.

If you are reading in a file with PHP code, and then evaluating that code, I'm not sure what you mean.

Normally, to include PHP code in a file, we use include() or include_once().

Can you paste the code and be more specific?
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

How to execute functions or initiate functions as command line parameters for below requirement?

I have 7 functions those need to be executed as command line inputs, I tried with below code it’s not executing function. If I run the ./script 2 then fun2 should execute , how to initiate that function I tried case and if else also, how to initiate function from command line if then... (8 Replies)
Discussion started by: saku
8 Replies

2. UNIX for Advanced & Expert Users

Embedding code into ssh keys

Hi Its been a long time since I worked with ssh keys containing embedded shell commands and cannot remember how it is done. Does anyone know of any sites that have a good tutorial on the subject? I'm not having much luck searching Google for it. Incidentally, searching this forum for the... (6 Replies)
Discussion started by: steadyonabix
6 Replies

3. Shell Programming and Scripting

Difficulty embedding variable within AWK

Hi, I am working on a parsing script but cannot figure out how to accomplish this. Here is a simplified version of the script: #!/bin/bash DS=$1 DS=`expr $DS \* 2` DS=`expr $DS + 7` cat $FILENAME | awk '/<row><v> +/' | awk '{printf("%.0f %.0f\n", $6, $9)}' The problem is that I want the... (2 Replies)
Discussion started by: Nisrak
2 Replies

4. Shell Programming and Scripting

Embedding HTML in Perl script

My webpage is hosted from perlscript(homepage.pl), i want to add piece of html code in the footer of the homepage. I simply pasted the html code at the end of the perl script as below... ======================================================== close(OUTSQL); ... (4 Replies)
Discussion started by: paventhan
4 Replies

5. UNIX Desktop Questions & Answers

Embedding file output into a script

Hello. I found a Unix script on this site that calculates a date that is 2 months earlier from today. I'm using that script and writing the value to a file called 2monthsago.txt. I want to use that value in another script. Below is my attempt at doing that and the results. My Script: ... (1 Reply)
Discussion started by: Colel2
1 Replies

6. Shell Programming and Scripting

CSH: Concatenating Strings, how to add new line character and functions?

Hello, I'm trying to run a program on a directory (traverse sub dirs too) through my csh script. Arrays support in CSH is appalling, something like associative arrays would have helped me do this so much easier. Anyway, I want to hold some details extracted from the program and then at the... (0 Replies)
Discussion started by: ragabonds
0 Replies

7. Shell Programming and Scripting

Embedding a command with SSH

Hi I am trying to run a script centrally that will go out and set the network management ip address on all my Sun boxes running Solaris. We have decided that the network management address will be the boxes main IP address but the first octet as a 172 rather than a 10, so for example ifconfig -a... (2 Replies)
Discussion started by: hcclnoodles
2 Replies

8. Programming

Embedding xnest in C code

I hope I am posting this in the right section. I have c file that is using the motif GUI toolkit to draw widgets and things of that sort. I also have another program that runs with xnest. I need to figure out a way to place that xnest program in my c code so that it exists in the window that the... (4 Replies)
Discussion started by: lesnaubr
4 Replies

9. Shell Programming and Scripting

Embedding Perl construct in ksh...

Hi, I have an embedded Perl construct in a korn script. However, I cannot seem to access the shell variables that were declared outside this Perl section. This is how my script is written....I have also tried back-ticks where I assign the shell variable to my local perl variable, still... (1 Reply)
Discussion started by: svetlur
1 Replies
Login or Register to Ask a Question