Php show label img in echo

1

I'm trying to show an employee's data; at the end I want to show your photo which is in the directory where the file is and has the extension .jpg

echo 'Hora de salida '.$horaDeSalida.'<br>'.
            'Valor Hora '.$valorHora.'<br>'.
            'Salario básico '.$salarioBasico.'<br>'.
            'Porcentaje Recargo Diurno '.$porcentajeRecargoDiurno.'<br>'.
            'Horas diurnas '.$horasDiurnas.'<br>'.
            'Recargo diurno '.$recargoDiurno.'<br>'.
            'Porcentaje Recargo Nocturno '.$porcentajeRecargoNocturno.'<br>'.
            'Horas nocturnas '.$horasNocturnas.'<br>'.
            'Recargo nocturno '.$recargoNocturno.'<br><br>'.

            'Salario neto '.$salarioNeto.'<br>'.
            'Nombre '.$_GET['Nombre'].'<br>'.
            '<img src=".$_GET['Nombre'].'.jpg'" width='50' height='50' />';     

But the last line does not fit me, I get the error: Parse error: syntax error, unexpected 'Name' (T_STRING), expecting ',' or ';'

    
asked by Jhon Hernández 07.06.2018 в 01:19
source

3 answers

1

In PHP we can declare a string in several ways if we need quotes in it, for example if we need a string to contain double quotes we do it this way echo '"Hola"'; and print "Hello", the same with the single quotes echo "'Hola'"; will print 'Hello'.

If we need to print a variable we can do it by encapsulating everything with double quotes, for example

$var = 'Soy un texto';
echo "$var";

It will give us as a result: I am a text

If we want to print two strings we can do this:

$var = 'Soy un texto';
$foo = ' Soy otro texto';
echo $var, $foo;

What will result in this: I am a text I am another text

Now your problem is that you are mixing several things

'<img src=".$_GET['Nombre'].'.jpg'" width='50' height='50' />';
----------^

So far so good, for PHP the double quote remains part of the string so continue

'<img src=".$_GET['Nombre'].'.jpg'" width='50' height='50' />';
------------------^

Until it arrives here, for PHP there we are closing the string, so expect us to close the line with ";" or that we put another string with a "," and that's why the error is so descriptive:

  

Parse error: syntax error, unexpected 'Name' (T_STRING), expecting ',' or ';'

Solution

We might want to encapsulate everything in double quotation marks and take advantage of the declaration of the variable in this way

"<img src='$_GET['Nombre'].jpg' width='50' height='50' />";

What would work for us if the variable did not have single quotes to access its index, which would take us back to the previous problems.

So, the solution is to concatenate the variable and print the tag

'<img src="'.$_GET['Nombre'].'.jpg" width="50" height="50" />';

Note that I put the numbers of width and height in double quotes, because if I do not do it we will return to the original problem.

    
answered by 07.06.2018 в 06:28
0

You have to escape the single quotes like this:

echo 'Hora de salida '.$horaDeSalida.'<br>'.
        'Valor Hora '.$valorHora.'<br>'.
        'Salario básico '.$salarioBasico.'<br>'.
        'Porcentaje Recargo Diurno '.$porcentajeRecargoDiurno.'<br>'.
        'Horas diurnas '.$horasDiurnas.'<br>'.
        'Recargo diurno '.$recargoDiurno.'<br>'.
        'Porcentaje Recargo Nocturno '.$porcentajeRecargoNocturno.'<br>'.
        'Horas nocturnas '.$horasNocturnas.'<br>'.
        'Recargo nocturno '.$recargoNocturno.'<br><br>'.

        'Salario neto '.$salarioNeto.'<br>'.
        'Nombre '.$_GET['Nombre'].'<br>'.
        '<img src="'.$_GET['Nombre'].'.jpg" width="50" height="50" />';     
    
answered by 07.06.2018 в 01:21
0

The echo must be defined on the same line without line breaks in the code, try this that you wrote ...

echo ('Hora de salida '.$horaDeSalida.'<br>'.'Valor Hora '.$valorHora.'<br>'.'Salario básico '.$salarioBasico.'<br>'.'Porcentaje Recargo Diurno '.$porcentajeRecargoDiurno.'<br>'.'Horas diurnas '.$horasDiurnas.'<br>'.'Recargo diurno '.$recargoDiurno.'<br>'.'Porcentaje Recargo Nocturno '.$porcentajeRecargoNocturno.'<br>'.'Horas nocturnas '.$horasNocturnas.'<br>'.'Recargo nocturno '.$recargoNocturno.'<br><br>'.'Salario neto '.$salarioNeto.'<br>'.'Nombre '.$_GET['Nombre'].'<br>'.'<img src=".$_GET['Nombre'].'.jpg'" width='50' height='50' />');
    
answered by 09.06.2018 в 23:18